How can I get a live reload like GAE with FALCON? - python

I am setting up a new project that is going to use python to build a RESTful back end. I looked at GAE, but choose Falcon Framework, because the application needs to eventually be installed on local servers. GAE has a great development feature, it allows for iterative development by watching the source, and reloading.
You can leave the web server running while you develop your
application. The web server knows to watch for changes in your source
files and reload them if necessary.
How can I set up Falcon to do the same thing?

This may not be the best answer, but I found that there is no simple method that does not require more software installed the way GAE does it, but after you install gunicorn, your can use the --reload switch and the server will auto-reload the source.
$ gunicorn -b 127.0.0.1:8000 -b [::1]:8000 --reload myapp:app
Docs: http://docs.gunicorn.org/en/19.0/settings.html#reload

Auto-reloading is not a feature of the framework (Falcon), but rather of the server. If you do want auto-reloading, the simplest way to do it is run your Falcon code on a gunicorn server, using the --reload switch. For example:
$ gunicorn --reload app:app
Assuming your API is inside app.py and named app.

Related

Should i use gunicorn with a python flaks application if i will put in a docker and then use it in a cloud environment

should i use gunicorn with a python flaks application if i will put in a docker and then use it in a cloud environment.
i have seen a lot of tutorials on how to do a flask application and deploy to a cloud service as a docker Image,
make the flask application
make the last line in docker with:
CMD ["python", "my_app.py"]
push the image and let the cloud service like AWS or azure use their load balancer with the amount of cpu and memeory you want to set as rule before spining another instance.
but i got into this tutorial and is using gunicorn
https://levelup.gitconnected.com/usage-of-gunicorn-for-deploying-python-web-applications-1e296618e1ab
where all the steps are the same, just the last line of the docker would be
CMD ["python" "-m", "gunicorn", "my_app:app"]
now uses the gnunicorn wsgi
and same way push that image to be use in the web application of the cloud.
i understand gunicorn can add more workers and thread as arguments in the cli; what would be the better approach or is one better than other?
Thanks guys.
Yes, you should always use a production server. As covered the the Flask docs, Deploy to Production: Run with a Production Server:
you should not use the built-in development server (flask run). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure.
Using gunicorn (or any other production WSGI server) is a necessary practice for deploying to production for speed, stability, and security. These concerns still persist, even if your app is deployed behind a load-balancer or proxy (like AWS load balancer). Both are necessary (you don't want to expose your WSGI server directly to the open internet, either, it should be behind a proper load balancer / proxy).

Can't find web process procfile error with Windows Heroku

I want to deploy my website which works locally, but runs an error when it tries to have a public instance.
I am on Windows 8.1 and following the official instructions have created a Procfile.windows. It looks like this:
web: python app.py runserver 0.0.0.0:5000
The build works, but then I get this error when I run heroku ps:scale web=1:
Couldn't find that process type (web).
Repo here.
The official tutorial works, so there's definitely proof of concept of this working somehow.
I am on Windows 8.1 and following the official instructions have created a Procfile.windows
Why did you name this file Procfile.windows? The documentation is quite clear:
The Procfile is always a simple text file that is named Procfile without a file extension. For example, Procfile.txt is not valid.
Rename it to Procfile and redeploy.
Once you've got that working, install a WSGI server and use that instead of manage.py runserver, which isn't suitable for production use:
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
Gunicorn is probably the most common choice, but I don't think it works on Windows so you won't be able to use it in your development environment. uWSGI or Waitress should work.
I installed Waitress and changed my Procfile to
web: waitress-serve --port=$PORT app:app
I also removed the .windows extension, which is meant for local development.
It works fine now!
(My equivalent to "main.py" was "app.py" so you'd likely change this for your needs to {main.py}:app)
Edit your procfile to look like
web: gunicorn --bind 0.0.0.0:$PORT app:app
And your procfile.windows filed as
web: python app.py runserver 0.0.0.0:5000

How to deploy a Django production server on IBM Cloud?

I want to know how to deploy a Django production server on IBM Cloud.
Right now I have a Cloud Foundry Python app running without problems. But I'm using python manage.py runserver to start the app on the cloud.
There is a way to run the app without using the django server? Like, how we do running that kind of apps using Apache o Nginx web servers.
UPDATE:
I tried using the Procfile and gunicorn, but it gives me an error when it starts gunicorn.
This is my Procfile.
web: gunicorn my_app.wsgi --workers 2
And the error
ERR Starting gunicorn 18.0
I am using python 3.6 and django 1.11
Cloud Foundry provides out-of-the-box production quality hosting. You don’t need to configure nginx or apache. For more information on how cloud foundry works under the hood, see here
You would however normally configure the method for starting your application. The python buildback documentation describes a few options, for example:
Specify a Start Command
The Python buildpack does not generate a
default start command for your applications.
To stage with the Python buildpack and start an application, do one of
the following:
Required for Cloud Foundry v245 only: Supply a Procfile.
For more
information about Procfiles, see the Configuring a Production Server
topic. The following example Procfile specifies gunicorn as the start
command for a web app running on Gunicorn:
web: gunicorn SERVER-NAME:APP-NAME
Specify a start command with -c.
The following
example specifies waitress-serve as the start command for a web app
running on Waitress:
$ cf push python-app -c "waitress-serve --port=$PORT DJANGO-WEB-APP.wsgi:MY-APP"
Specify a start command in the application manifest by setting the command attribute.
For more
information, see the Deploying with Application Manifests topic.

Run Django with python as daemon

We have web application which is running with django, python and PostgreSQL. We are also using virtualenv.
To start the web service, we first activate the virtualenv and then start python as service on 8080 with nohup.
But after sometime nohup process dies. Is there any way to launch service as demon like apache, or use some thing like monit?
I am new to this, please excuse my mistakes
So a runserver command should only be used in testing environments. And just like #Alasdair said, Django docs already have interesting information about that topic.
I would suggest using gunicorn as a wsgi with nginx as a reverse proxy. You can find more information here
And i would suggest using supervisor to monitor and control your gunicorn workers. More information can be found here
It may be a good idea to deploy your application using apache or ngnix. There is official Django documentation on how to do it with apache - https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/
Apache does support virtual environment - just add python-home=<path_to_your_virtual_env> to the WSGIDaemonProcess directive when using daemon mode of mod_wsgi:
WSGIDaemonProcess django python-path=/opt/portal/src/ python-home=/opt/venv/django home=/opt/portal/
Best practice for how to use mod_wsgi and virtual environments is explained in:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
I was able to do it, but forgot to update the answers.IF any one is looking for same they can follow this.
Best way to run django app in production is to run with
django+gunicorn+supervisor+nginx.
I used gunicorn which is a Python WSGI HTTP Server for UNIX where you can control thread count, timeout settings and much more. gunicorn was running was on socket, it could be run on port but to reduce tcp overhead we ran on socket.
Supervisor is used to run this gunicorn script as supervisor is simple tool which is used to control your process.
and with the help of nginx reverse proxy Our Django site was life.
For more details follow below blog.
http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/

Do i need to use apache or nginx to host a server?

Do i need to use NginX or am i able to host it without it?
I am developing my first django project and am at the point where i can run the app project using the command:
./manage.py run_gunicorn -c config/gunicorn
I can then view it going to:
http://127.0.0.1:8000/resources/
I would now like to try hosting it so that other PCs can access this.
Gunicorn is wsgi http server. It is best to use Gunicorn behind HTTP proxy server. We strongly advise you to use nginx.
# http://gunicorn.org/#deployment
Although there are many HTTP proxies available, we strongly advise that you use Nginx. If you choose another proxy server you need to make sure that it buffers slow clients when you use default Gunicorn workers. Without this buffering Gunicorn will be easily susceptible to denial-of-service attacks.
# http://docs.gunicorn.org/en/latest/deploy.html
Of course not. You can use lighttpd or any other web server that supports WSGI, SCGI, FastCGI or AJP. You may refer to this python documentation and django documentation, and these two questions on stackoverflow: Cleanest & Fastest server setup for Django, Differences and uses between WSGI, CGI, FastCGI, and mod_python in regards to Python? might be also helpful.
You don't need a frontend proxy; you can put a standalone webserver like gunicorn directly in production. But there are various reasons why you probably want to use a frontend webserver anyway.

Categories

Resources