Created
August 4, 2011 21:13
-
-
Save teh/1126302 to your computer and use it in GitHub Desktop.
Proper shut down in-flight requests for eventlet's wsgi server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import daemon, lockfile, signal, logging | |
import eventlet | |
from eventlet import wsgi, timeout | |
worker_pool = eventlet.GreenPool(20) | |
sock = eventlet.listen(('', 8000)) | |
def proper_shutdown(): | |
worker_pool.resize(0) | |
sock.close() | |
logging.info("Shutting down. Requests left: %s", worker_pool.running()) | |
worker_pool.waitall() | |
logging.info("Exiting.") | |
raise SystemExit() | |
def queue_shutdown(signal_number, stack_frame): | |
eventlet.spawn_n(proper_shutdown) | |
def test_app(evn, start_response): | |
start_response('200 OK', {}) | |
eventlet.sleep(20) | |
return ['hi'] | |
# Daemon things | |
context = daemon.DaemonContext( | |
working_directory='.', | |
umask=0o002, | |
pidfile=lockfile.FileLock('shutdown-example.pid'), | |
) | |
context.signal_map = { | |
signal.SIGTERM: queue_shutdown, | |
} | |
context.files_preserve = [sock] | |
with context: | |
logging.basicConfig(filename='shutdown.log', level=logging.INFO) | |
logging.info("Starting") | |
wsgi.server(sock, test_app, custom_pool=worker_pool) |
Hey apatrushev. eventlet's wsgi enables keep alive by default, so you should not have to do anything.
With keep alive enabled (by default) this code doesn't work (connections
not ended when request processing ends).
, 23 2012 . teh :
… Hey apatrushev. eventlet's wsgi enables keep alive by default, so you
should not have to do anything.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1126302
Apologies, I misunderstood your question.
If you have keepalive connections open you'll need to be a bit harsher, maybe with the following (untested!) code:
worker_pool.resize(0)
sock.close()
eventlet.sleep(2) # Give running requests 2 seconds to shut down
for coro in worker_pool.coroutines_running:
coro.kill()
No need to close the socket as wsgi.server() closes it upon exit.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to fix this code to force end keep alive connections ?