Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are uwsgi threads used for?

Tags:

python

uwsgi

wsgi

I see in a uwsgi.ini file there is a configuration

[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191

I understand that each request is served in a different process. Then what are threads used for ?

like image 211
Anubhav Agarwal Avatar asked Dec 09 '15 15:12

Anubhav Agarwal


People also ask

What is the use of uWSGI?

uwsgi (all lowercase) is the native binary protocol that uWSGI uses to communicate with other servers. uWSGI is often used in conjunction with web servers such as Cherokee and Nginx, which offer direct support for uWSGI's native uwsgi protocol, to serve Python web applications such as Django.

Is uWSGI multi threaded?

Running uWSGI in multithreading mode (with the threads options) will automatically enable threading support.

How many requests can uWSGI handle?

yes, every single thread can manage a single request, so if you have 3 processes and 5 threads you can manage 15 concurrent requests. Save this answer. Show activity on this post. when hosting python behind uWSGI, it can only run as many simultaneous requests as processes there are.

What is the difference between uWSGI and WSGI?

WSGI is a specification. uWSGI provides an implementation of the WSGI specification. You can't compare them. You can only compare different implementations.


2 Answers

Adding an additional answer here due to a huge GOTCHA I had today.

Shared memory works across threads, but not processes. That is, if you have some module level thing like:

# mymodule
mycache = {}

mycache[key] = value

del mycache[key]

...

Deletes in one process will NOT be reflected in the cache of another process. However, deletes in one thread, if only one process is used, will persist across threads.

So if you are using shared memory like this, you have two options:

  1. all caches should be "safe" and "read through" (ie on cache miss, try to load the real data)
  2. always run threads=X but processes=1
like image 41
Tommy Avatar answered Sep 21 '22 00:09

Tommy


Both processes and threads can be used for increasing concurrency. Threads are cheaper than processes and use less resources, but may not always run in parallel because of Python GIL.

Also, quoting the uWSGI documentation:

There is no magic rule for setting the number of processes or threads to use. It is very much application and system dependent. Simple math like processes = 2 * cpucores will not be enough. You need to experiment with various setups and be prepared to constantly monitor your apps. uwsgitop could be a great tool to find the best values.

like image 143
Eugene Yarmash Avatar answered Sep 21 '22 00:09

Eugene Yarmash