Answer the question
In order to leave comments, you need to log in
How to respond to the client in the request handler and continue executing the code?
A small server on a tornado, an example of a request handler:
class MessageHandler(tornado.web.RequestHandler):
@gen.coroutine
def post(self):
if self.request.body == 'aaa':
self.write("Запрос получен")
self.finish()
MyFunc() # Функция которая может выполняться длительное время
else:
self.write("Неверный запрос")
Answer the question
In order to leave comments, you need to log in
1 To answer the client - "everything is fine" before the actual execution of the request - in principle, it is not correct. What if something goes wrong? and you have already told the client that everything is fine.
2 into a separate thread or a separate process? the correct answer depends on the type of operations performed, if you have complex calculations there, then the stream will not suit you, it is useful for I / O operations. Please note that the implementation of parallelism in windows and unix will be different.
in general, in essence, it’s right to make such decisions through the queue manager, they put synchronous tasks into a certain microservice, then you can ensure the persistence of tasks and give the correct answer to the user and not worry that the main thread will be blocked.
Well, to summarize, of course, I don’t know the reasons for choosing this technology, but now there is a ready-made implementation of asynchrony in python. I'm talking about asyncio, of course it won't get rid of blocking problems for you, but it will allow you to solve asynchrony issues in a more elegant way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question