python flask 多线程_flask开启多线程的具体方法

本文详细介绍了如何在Flask中开启多线程,通过分析`app.run()`和`run_simple()`函数,揭示了Flask如何通过`ThreadedWSGIServer`实现多线程。默认情况下,Flask是单线程运行,但通过设置`threaded=True`参数,可以在开发环境中启用多线程。生产环境中通常使用uWSGI或Gunicorn等服务器进行部署。
摘要由CSDN通过智能技术生成

在我之前解释了flask如何支持多线程主要通过两个类来实现,LocalStack和Local,在Local中有两个属性,__storage__和__ident_func__,后者用来获取线程id,从而区分不同线程发来的请求

这次要说的是flask如何开启多线程

先从app.run()这个方法看起

def run(self, host=None, port=None, debug=None, **options):

from werkzeug.serving import run_simple

if host is None:

host = '127.0.0.1'

if port is None:

server_name = self.config['SERVER_NAME']

if server_name and ':' in server_name:

port = int(server_name.rsplit(':', 1)[1])

else:

port = 5000

if debug is not None:

self.debug = bool(debug)

options.setdefault('use_reloader', self.debug)

options.setdefault('use_debugger', self.debug)

try:

run_simple(host, port, self, **options) #会进入这个函数

finally:

# reset the first request information if the development server

# reset normally. This makes it possible to restart the server

# without reloader and that stuff from an interactive shell.

self._got_first_request = False

经过判断和设置后进入run_simple()这个函数,看下源码

def run_simple(hostname, port, application, use_reloader=False,

use_debugger=False, use_evalex=True,

extra_files=None, reloader_interval=1,

reloader_type='auto', threaded=False,

processes=1, request_handler=None, static_files=None,

passthrough_errors=False, ssl_context=None):

"""Start a WSGI application. Optional features include a reloader,

multithreading and fork support.

This function has a command-line interface too::

python -m werkzeug.serving --help

.. versionadded:: 0.5

`static_files` was added to simplify serving of static files as well

as `passthrough_errors`.

.. versionadded:: 0.6

support for SSL was added.

.. versionadded:: 0.8

Added support for automatically loading a SSL context from certificate

file and private key.

.. versionadded:: 0.9

Added command-line interface.

.. versionadded:: 0.10

Improved the reloader and added support for changing the backend

through the `reloader_type` parameter. See :ref:`reloader`

for more information.

:param hostname: The host for the application. eg: ``'localhost'``

:param port: The port for the server. eg: ``8080``

:param application: the WSGI application to execute

:param use_reloader: should the server automatically restart the python

process if modules were changed?

:param use_debugger: should the werkzeug debugging system be used?

:param use_evalex: should the exception evaluation feature be enabled?

:param extra_files: a list of files the reloader should watch

additionally to the modules. For example configuration

files.

:param reloader_interval: the interval for the reloader in seconds.

:param reloader_type: the type of reloader to use. The default is

auto detection. Valid values are ``'stat'`` and

``'watchdog'``. See :ref:`reloader` for more

information.

:param threaded: should the process handle each request in a separate

thread?

:param processes: if greater than 1 then handle each request in a new process

up to this maximum number of concurrent processes.

:param request_handler: optional parameter that can be used to replace

the default one. You can use this to replace it

with a different

:class:`~Bas

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值