I have a request in which I have to have a blocking call in tornado server and I do not want the main thread to be blocked for any reason. So I thought I will run it in a different thread/process.
The stub code is like this:
import tornado.web
import tornado.gen
import time
from tornado.ioloop import IOLoop
## Run this function in different process
def blocking_get(var1):
print("blocking function")
time.sleep(2)
return {"res":"some result"}
class rootHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
print("Get called for {}".format(self))
var1 = self.get_argument('var1', None)
## ************************************
## get values from blocking call blocking_get()
resp_dict = getRespFromDifferentProcess()
self.write(resp_dict)
self.finish()
def _call_later_something(self):
print("_call_later_something")
class TestApp(tornado.web.Application):
def __init__(self, test=False):
handlers = [
(r"/", rootHandler),
]
tornado_settings = dict(
debug=True,
serve_traceback=True,
)
tornado.web.Application.__init__(self, handlers, **tornado_settings)
if __name__ == "__main__":
PORT = 8888
print("Tornado on port {}".format(PORT))
http_server = TestApp()
http_server.listen(PORT)
IOLoop.instance().start()
- If I run it in different thread will the GIL of python block the main thread while it is executing the blocking thread?
- What is the advantage of using multi-processing over multi-threading?
- If I have to use multi-processing in tornado how to do that?
- If I want the blocking code to take the values from a queue, process them and call the function in the main thread how do I have to implement it.