S
S
sinbadxiii2016-05-19 10:41:51
Python
sinbadxiii, 2016-05-19 10:41:51

Is it possible to run a method in the background in tornado?

Good afternoon.
There is a task on get request to give the data from a DB. But if the database does not have the desired value, then give the answer to self.write, the user will receive the loaded page, and at this time run the parser in the background, which will get the desired values ​​​​and write them to the database. And with a second request, the response will already be the recorded data.
Should I look towards Asynchronous Requests? Type:

from tornado.httpclient import AsyncHTTPClient

def asynchronous_fetch(url, callback):
    http_client = AsyncHTTPClient()
    def handle_response(response):
        callback(response.body)
    http_client.fetch(url, callback=handle_response)

Or nevertheless, here it is necessary to connect streams?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Konovalov, 2016-05-19
@akonovalov

Based on the assumption that such cases of missing data are not rare, and parsing with adding data to the database takes a significant amount of time and the user can wait, then I would probably do the following.
1) I would start a persistent job queue, for example, using Redis, or I would use an existing database.
2) Using the PeriodicCallback mechanism, I would periodically check this queue and ...
3) I would start parsing through Subprocess or joblib
You can do all this inside Tornado, adding tasks to IOLoop - but there is a risk that at some point the server will be busy mainly by parsing, and not by processing requests from the frontend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question