Nginx – Python Threads and uWSGI Harakiri

djangonginxthreadsuwsgi

I have a webapp that is using an API built on nginx, uWSGI, and Django. Recently I have been seeing a lot of harakiris on a particular API call which does some quick processing and then spawns a few threads to handle some long-running functions. The main thread then returns and the worker threads store their results in a cache and exit. After the threads are spawned, there is no interaction between them.

My uWSGI is set to harakiri at 20 seconds. This should be ample time for the main thread to finish and return (it's average is about 2-3 seconds). Is it possible that uWSGI is harakiri-ing because my worker threads are taking too long to return? If so, could I fix this by using multiprocessing instead of multithreading in python?

Thanks!

Best Answer

These options will come in very handy to figure out what exactly is going on with the harakiri feature:

harakiri-verbose=true

enable verbose mode for harakiri

py-tracebacker=/path/to/socket

enable the uWSGI python tracebacker

Read from that socket to see the traceback (uWSGI conveniently ships a --connect-and-read=/path/to/socket option).

You can also investigate the post-request thread status and log diagnostics by setting uwsgi.after_req_hook = callback. Before this method is called the harakiri mode can explicitly disabled:

harakiri-no-arh=true

do not enable harakiri during after-request-hook