简体   繁体   中英

simple multithreading example with tornado.web.RequestHandler

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.

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:

http://www.tornadoweb.org/en/stable/faq.html#my-code-is-asynchronous-but-it-s-not-running-in-parallel-in-two-browser-tabs

... 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM