Python Celery versus Threading Library for running async requests [closed]

℡╲_俬逩灬. 提交于 2019-12-03 15:43:51

问题


I am running a python method that parses a lot of data. Since it is time intensive, I would like to run it asynchronously on a separate thread so the user can still access the website/UI.

Do threads using the "from threading import thread" module terminate if a user exits the site or do they continue to run on the server?

What would be the advantages of using Celery versus simply using the threading module for something like this?


回答1:


Python threads are not true native OS threads. See Green-threads and thread in python.

And, the Python interpreter is resolutely single threaded due to the (in)famous global interpreter lock aka (GIL). Thus threading in Python only provides parallelism when computation and IO can happen simultaneously. Compute bound tasks will see little benefit from threading in the Python threading model, at least under CPython 2 or 3.

On the other hand, those limitations don't apply to multiprocessing, which is what you'll be doing with a queuing system like celery. You can run several worker instances of Python which can execute simultaneously on multicore machines, or multiple machines for that matter.

If I understand your scenario - an interaction on a web site kicks off a long-running job - queuing is almost certainly the way to go. You get true parallelism and the easy option to move processing to other machines.



来源:https://stackoverflow.com/questions/23796837/python-celery-versus-threading-library-for-running-async-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!