A very simple tornado app, when server receive a HTTP get request, it ping -c 2 www.google.com
, then return the result. And I want use Tornado. Here is the code from an article.
class AsyncTaskHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
response = yield tornado.gen.Task(self.ping, ' www.google.com')
print 'response', response
self.finish('hello')
@tornado.gen.coroutine
def ping(self, url):
os.system("ping -c 2 {}".format(url))
return 'after'
and the author says that the ab test result is awesome.
ab -c 5 -n 5 http://127.0.0.1:5000/async
Document Path: /async
Document Length: 5 bytes
Concurrency Level: 5
Time taken for tests: 0.009 seconds
Complete requests: 5
Failed requests: 0
Total transferred: 985 bytes
HTML transferred: 25 bytes
Requests per second: 556.92 [#/sec] (mean)
Time per request: 8.978 [ms] (mean)
Time per request: 1.796 [ms] (mean, across all concurrent requests)
Transfer rate: 107.14 [Kbytes/sec] received
but really I use just the same code, and in my test, Requests per second is 0.77! I search for the reason. And I found this version:
class IndexHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(10)
@tornado.gen.coroutine
def get(self):
print "begin"
response = yield self.pin()
print response
self.finish()
@run_on_executor
def pin(self):
return os.system("ping -c 2 www.google.com")
And the test result, Requests per second is 0.85. I wanna use tornado coroutine to make 1000 or more ping commands no-blocking. How can I code it? Thanks a lot!