Reputation: 3742
I have got a mysterious_library
, providing a synchronous function query_resource_for_a_long_time
.
Then I have the code below that is supposed to fetch the resource asynchronously:
import tornado.ioloop
import tornado.web
import threading
from mysterious_library import query_resource_for_a_long_time, ResourceNotFoundException
def resource_fetcher(set_status, finish):
try:
resource = query_resource_for_a_long_time()
except ResourceNotFoundException:
tornado.ioloop.IOLoop.instance().add_callback(set_status, 404)
tornado.ioloop.IOLoop.instance().add_callback(finish, 'not found')
else:
tornado.ioloop.IOLoop.instance().add_callback(set_status, 200)
tornado.ioloop.IOLoop.instance().add_callback(finish, str(resource))
class Handler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
threading.Thread(
target=resource_fetcher,
args=[self.set_status, self.finish]
).start()
tornado.web.Application([
(r'.*', Handler),
]).listen(8765)
tornado.ioloop.IOLoop.instance().start()
However, it seems that the process is blocked until query_resource_for_a_long_time
returns, although the function runs in a separated thread.
I'm new to tornado and I'm wondering is it possible to deal with these requests concurrently.
Upvotes: 1
Views: 8521
Reputation: 24007
Yes, follow the instructions to use ThreadPoolExecutor:
http://www.tornadoweb.org/en/stable/guide/coroutines.html#calling-blocking-functions
Be aware, when you're testing this, that you can only run a couple queries at once from your browser:
... so try wget or curl if you want to prove to yourself that you can run the mysterious long-running function in many threads at once from Tornado.
Upvotes: 3