Long time no Update blog, the previous period busy with the completion of the establishment, and then to the company internship. In the near period of time empty, tidy up the problems encountered before, as well as the solution.
Usually when the computer is running long code, it needs to be placed in the background for processing, that is, to implement asynchronous. One solution in Python is to use the celery package.
The implementation of the idea is that before the calculation in the database such as MONGO insert a {"state": "0"};celery put the calculation program into a database to store messages, usually can use Redis; at the same time, start celery worker, it will keep check The message inside the Redis, if any, is calculated, and when the update record {"state": "1", "result": "Something"} is completed.
The advantage of Asynchrony is the ability to put a bunch of calculation in the background, while the front end just keeps check the state of that record feature. Of course state can also have a lot of status values, you can define each error, so you can also find errors.
Python Asynchronous processing celery