Skip to content

Instantly share code, notes, and snippets.

@anton-ryzhov
Created June 20, 2014 08:27
Show Gist options
  • Save anton-ryzhov/05e3211d0050d8e8f8fc to your computer and use it in GitHub Desktop.
Save anton-ryzhov/05e3211d0050d8e8f8fc to your computer and use it in GitHub Desktop.
WSGIContainer vs ExecutorWSGIContainer
#!/usr/bin/env python
# Setup config below
# Call simultaneously some:
# $ curl http://localhost:8888/
import random
import sys
import time
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
from concurrent.futures import ThreadPoolExecutor
### Config ###
## Send answer fit to output buffer or overflows it and sent chunk-by-chunk
big_packet = False
## Container type:
ctype = 'basic'
#ctype = 'dummy'
#ctype = 'threaded'
def simple_app(environ, start_response):
simple_app.count += 1
num = simple_app.count
print 'Start long processing #', num
time.sleep(5)
print 'End of long processing #', num
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
start_response(status, response_headers)
for i in range(1000000 if big_packet else 100):
yield random.choice('abcdef')
yield '\n'
print 'End of request #', num
print
simple_app.count = 0
def alive():
print 'IOLoop alive'
with ThreadPoolExecutor(1) as executor:
containers = {
'basic': tornado.wsgi.WSGIContainer(simple_app),
'dummy': tornado.wsgi.ExecutorWSGIContainer(simple_app),
'threaded': tornado.wsgi.ExecutorWSGIContainer(simple_app, executor=executor),
}
http_server = tornado.httpserver.HTTPServer(containers[ctype])
http_server.listen(8888)
tornado.ioloop.PeriodicCallback(alive, 1000).start()
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment