Vingtoft
Vingtoft

Reputation: 14606

Background threads not starting before server shutdown

I'm having some trouble getting simple multi-threading functionality up and running in my web application.

Im using Flask, uwsgi, nginx on Ubuntu 12.04.

Each time I start a new thread it will not execute before I shut down the uwsgi server. Its very odd!

If I'm doing a simple task (e.g. printing) it will execute as expected 9/10 times. If I do a heavy computing job (e.g. OCR on a file) it will always start executing when the server is restarting (doing a shutdown)

Any idea why my code does not perform as expected?

Code:

def hello_world(world):
    print "Hello, " + world # This will get printed when the uwsgi server restarts

def thread_test():
    x = "World!"
    t = threading.Thread(target=hello_world, args=(x,))
    t.start()

@application.route('/api/test')
def test():
    thread_test()
    return "Hello, World!", 200

EDIT 1:

My uwsgi configuration looks like this:

[uwsgi]
chdir       = /Users/vingtoft/Documents/Development/archii/server/archii2/
pythonpath  = /Users/vingtoft/Documents/Development/archii/server/archii2/
pythonpath  = /Users/vingtoft/Documents/Development/archii/server/ml/
module      = app.app:application
master      = True
vacuum      = True
socket      = /tmp/archii.sock 
processes   = 4
pidfile     = /Users/vingtoft/Documents/Development/archii/server/archii2/uwsgi.pid 
daemonize   = /Users/vingtoft/Documents/Development/archii/server/archii2/uwsgi.log
virtualenv  = /Users/vingtoft/Documents/Development/virtualenv/flask/
wsgi-file   = /Users/vingtoft/Documents/Development/archii/server/archii2/app/app.py
ssl         = True

Upvotes: 1

Views: 319

Answers (1)

GwynBleidD
GwynBleidD

Reputation: 20539

uWSGI server by default will disable threads support for some performance improvements, but you can enable it back using either:

threads = 2 # or any greater number

or

enable-threads = true

But be warned that first method will tell uWSGI to create 2 threads for each of your workers so for 4 workers, you will end up with 8 actual threads.

That threads will work as separate workers, so they are not for your use for background jobs, but using any number of threads greater than one will enable thread support for uWSGI server, so now you can create more of it for some background tasks.

Upvotes: 3

Related Questions