Answer a question

I have a pretty straightforward question, but I haven’t found a very straightforward answer: When should I use start_background_task() over starting a Python thread “normally”? Flask-SocketIO documentation states:

This function returns an object compatible with the Thread class in the Python standard library. The start() method on this object is already called by this function.

It doesn’t say much about whether it’s necessary to use this over initializing and starting a thread per the threading module.

Answers

The reason start_background_task() exists is that depending on what web server you use the threading model changes. For example, if you are using eventlet or gevent, then a background task needs to be started as a greenlet, not as a Thread instance.

If you use start_background_task() you will be guaranteed that a task object that is compatible with your environment will be used.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐