Serverless and NewRelic - python

I'm using the latest version of the Serverless framework and am trying to integrate NewRelic into my applications. I have both Django as well as Flask applications (-> Python!) which I'd like to monitor with NewRelic. I integrated the framework as documented (by wrapping the WSGI application) and set the NewRelic timeout variables accordingly to make sure data is getting sent.
In my NewRelic dashboard I'm able to see the project but no data is reported. Has anyone had success getting this running?
Thanks!

It might be worth trying the manual instrumentation in your main app file instead of the wsgi file to see if thats working.
import newrelic.agent
newrelic.agent.initialize('/some/path/newrelic.ini')
... YOUR_OTHER_IMPORTS
https://docs.newrelic.com/docs/agents/python-agent/installation-configuration/python-agent-integration#manual-integration
If that works, work backwards towards the wsgi file :) (be sure to restart your app)

Related

Which production server to use for Python applications in Cloud Run?

I want to use GCP cloud run as a technology to run my python flask app, so I have to dockerize it. Most of examples I've seen are either using built in flask server or gunicorn server as an ENTRYPOINT, which gives a warning on a console, that it shouldn't be used on production.
My question is: does it matter with a platform like GCP cloud run which server do I use to run that code? What would be the performance impact of that choice?
You want gunicorn, and you'll need to configure it correctly.
Typically in these setups there will be an external HTTP server proxying requests to your server. So it matters rather less which webserver you're using on the backend, because it's not directly exposed.
That being said, the built-in Flask webserver isn't ideal, so gunicorn would probably be better. You will need to tweak Gunicorn's settings slightly to work correctly in container: logging, heartbeat setting, and parallelism.
See https://pythonspeed.com/articles/gunicorn-in-docker/ for details.

does it make sense to use apache as web server for django in development mode

I'm a newbie in django and have a project that involves distributed remote storage and I'm suggested to use mod x-sendfile as part of the project procedure.
In have a django app that receives a file and transforms it into N segments each to be stored on a distinct server. Those servers having a django app receiving and storing the segments.
But since mod x-sendfile works need apache and I am just in developing and trying stage this question occurred to me.
I googled a lot but found nothing in that regard.
So my question being: Is it possible to use apache as django web server during the development of django apps? Does it make sense in development mode to replace apache with django built-in web server?
There should be nothing stopping you from installing a copy of Apache on your workstation and using it for developing, and since you're working on something that depends on some of that functionality it makes perfect sense for you to use that for your development server instead of ./manage.py runserver.
Most people use Djangos built-in server because they don't need more than that for what they're trying to do - it sounds like your solution does.
Heck, since you're testing distributed you may even want to consider grabbing a virtualization tool (qemu, virtualbox, et al) so you can have a faux-distributed setup to work with (I'd suggest doing a bit of scripting to make it easy to deploy / restart them all at once though - it'll save you from having to track down issues where the code that's running is older than you thought it was).
Your development environment can be what you need it to be for what you're doing.

How to run nginx + python (without django)

I want to have simple program in python that can process different requests (POST, GET, MULTIPART-FORMDATA). I don't want to use a complete framework.
I basically need to be able to get GET and POST params - probably (but not necessarily) in a way similar to PHP. To get some other SERVER variables like REQUEST_URI, QUERY, etc.
I have installed nginx successfully, but I've failed to find a good example on how to do the rest. So a simple tutorial or any directions and ideas on how to setup nginx to run certain python process for certain virtual host would be most welcome!
Although you can make Python run a webserver by itself with wsgiref, I would recommend using one of the many Python webservers and/or web frameworks around. For pure and simple Python webhosting we have several options available:
gunicorn
tornado
twisted
uwsgi
cherrypy
If you're looking for more features you can look at some web frameworks:
werkzeug
flask
masonite
cherrypy (yes, cherrypy is both a webserver and a framework)
django (for completeness, I know that was not the purpose of the question)
You should look into using Flask -- it's an extremely lightweight interface to a WSGI server (werkzeug) which also includes a templating library, should you ever want to use one. But you can totally ignore it if you'd like.
You can use thttpd. It is a lightweight wsgi server for running cgi scripts. It works well with nginx. How to setup thttpd with Nginx is detailed here: http://nginxlibrary.com/running-cgi-scripts-using-thttpd/
All the same you must use wsgi server, as nginx does not support fully this protocol.

Flask unresponsive while uploading file or waiting for other server

My Flask stops responding when uploading files or when collecting data from another server via GET. I assume, the problem is, that Flask is only running on one thread.
How can I change this, so multiple users can use the site?
Flask's development webserver (invoked when you use app.run) is not a production web server.
Quoting the docs:
You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)
If you want to use Flask in a production environment take a look at the deployment options suggested by the documentation.
For testing purposes with small applications that are doing slightly complicated things I deploy the code I'm developing behind CherryPy using this snippet. (The only disadvantage of this pattern is you loose access to Werkzeug's debugger.)

A production ready server to serve django on win32

I'd like to serve django application on windows XP/Vista.
The application is an at hoc web interface to a windows program so it won't be put under heavy load (around 100 requests per second).
Do you know any small servers that can be easily deployed on windows to serve a django app? (IIS is not an option as the app should work on all versions of windows)
cherrypy includes a good server. Here's how you set it up to work with django and some benchmarks.
twisted.web has wsgi support and that could be used to run your django application. Here's how you do it.
In fact any wsgi server will do. Here's one more example, this time using spawning:
$ spawn --factory=spawning.django_factory.config_factory mysite.settings
And for using paste, the info is gathered here.
Of course, you could use apache with mod_wsgi. It would be just another wsgi server. Here are the setup instructions.
If you want to give Apache a go, check out XAMPP to see if it'll work for you. You can do a lightweight (read: no installation) "installation." Of course, you'll also want to install mod_python to run Django. This post may help you set everything up. (Note: I have not used python/Django with XAMPP myself.)
Edit: Before someone points this out, XAMPP is not generally a production-ready tool. It's simply a useful way to see whether Apache will work for you. Also, I saw that you're using SQLite after the fact.
Why not Apache ?
Nokia have developed a scaled down version of apache to run on their mobile phones. It supports python.
http://research.nokia.com/research/projects/mobile-web-server/
Also do you need anything else such as database support etc?

Categories

Resources