Search code examples
multithreadingpython-multithreadinguwsgiwsgi

Unable to make an HTTP request from a uwsgi worker thread


I am making an HTTP request from a worker thread in my Python application.

It works when I run the Python application on localhost. But when I run the Python application on a uwsgi production server it does not work.

I am using the requests module to make the HTTP request, if that helps.

Why does uwsgi not allow me to make an HTTP request in a worker thread?

My uwsgi config includes these statements:

[uwsgi]
master = true
processes = 4
threads = 4
enable-threads = true
lazy-apps = true
module = # name of main module
callable = app
wsgi_file = # path to main
pyargv = # argv
logto = # logfile
venv = # path to venv
chdir = # app directory
user = uwsgi
plugins = python36

Is there anything else I have to add to my uwsgi config file so that I can make HTTP requests from worker threads using the requests module?

How can I make my uwsgi production server behave properly, so that I can make HTTP requests from worker threads?

(It works fine, after all, on a Flask development server.)

Thanks for any help on this subject.


Solution

  • I was able to answer my question.

    I was running a line of code that is not thread-safe, causing the thread to exit.

    So the problem was not caused by my uwsgi config file.

    Instead, the problem was caused by a line of code that is not thread-safe.

    I was trying to access the Flask session object from inside a worker thread, and this operation is not thread-safe. To fix the problem, I simply passed my worker thread a variable that it needs, so that it doesn't have to access the Flask session object.