[PYTHON] run uwsgi server in uwsgi-gevent mode

http://uwsgi-docs.readthedocs.io/en/latest/Gevent.html?highlight=gevent While watching, try running uwsgi in gevent mode.

environment

nginx: 1.10.1 python: 2.7.10 uwsgi: 2.0.13.1 Since it is built locally, nginx-uwsgi communicates on Unix domain sockets.

uwsgi settings

Added gevent parameter

uwsgi.ini


[uwsgi]
...
gevent = 40

The value here is the number of workers in gevent, but I don't know the optimal value. (I want someone to tell me)

MySQL module

It seems that gevent does not support the MySQL connector if it is MySQL Python. Use PyMySQL. Also, if you use Django, if you use PyMySQL as it is, it will not mesh with the backend implementation, so at startup,

import pymysql
pymysql.install_as_MySQLdb()

You need to do.

Monkey patch

At the beginning of the uwsgi startup module,

import gevent.monkey
gevent.monkey.patch_all()

Apply the monkey patch with. (To be executed before other imports) It seems that gevent-monkey-patch = true can also be used in uwsgi.ini, In my environment, I got an error around MySQL and it didn't work. Perhaps it's related to the internal import order.

Run

Run uwsgi as usual,

*** running gevent loop engine [addr:0x10e228440] ***

If a message like this is displayed, the startup is successful in gevent mode. After that, try HTTP request and if no error occurs, it's OK.

Try to start

It is hard to realize the difference between no gevent and not (because it is local). I still don't know the optimum value of the parameter, so I will try various things and verify it.

Recommended Posts

run uwsgi server in uwsgi-gevent mode
Use Juman ++ in server mode
Run the task in the background on the sshed server
Run CGI written in python on Sakura's rental server
Until you run server Django in Visual Studio Code
DNS server in Python ....
Setting to run application in subdirectory with nginx + uwsgi
Asynchronously run matplotlib interactive mode
Run automatic jobs in python
Run shell commands in python
Run Python unittests in parallel
Run background job in fabric
Run Amazon Linux 2 in VirtualBox
Using Python mode in Processing