Skip to content

Instantly share code, notes, and snippets.

@pior
Created March 6, 2016 01:39
Show Gist options
  • Select an option

  • Save pior/a494e75efa1ee45a4033 to your computer and use it in GitHub Desktop.

Select an option

Save pior/a494e75efa1ee45a4033 to your computer and use it in GitHub Desktop.
Using a multiprocessing.pool.ThreadPool wih uWSGI -- Tested with gevent
from gevent.monkey import patch_all
patch_all()
import time
from multiprocessing.pool import ThreadPool
import uwsgi
emitter = ThreadPool(10)
def goodbye():
print " ---- Shutdown worker %d" % uwsgi.worker_id()
emitter.close()
print " ---- Waiting emitter to close"
emitter.join()
print " ---- Emitter closed"
uwsgi.atexit = goodbye
def long_task():
print " -- IN"
time.sleep(5)
print " -- OUT"
def application(env, start_response):
emitter.apply_async(long_task)
start_response('200 OK', [('Content-Type', 'text/html')])
return [b"Hello World"]
# uwsgi --http :9090 --wsgi-file app.py --master-fifo uwsgi-fifo --enable-threads --lazy-apps --gevent 10
# Graceful shutdown :
# echo q > uwsgi-fifo
@yangmillstheory
Copy link

Do you know why the lazy-apps option is needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment