gpt4 book ai didi

python - 将 concurrent.futures 与 Flask 结合使用会提高性能吗

转载 作者:行者123 更新时间:2023-11-28 21:48:39 24 4
gpt4 key购买 nike

我想知道在 Flask 中使用 concurrent.futures 是否可以。这是一个例子。

import requests
from flask import Flask
from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=10)
app = Flask(__name__)

@app.route("/path/<xxx>")
def hello(xxx):
f = executor.submit(task, xxx)
return "OK"

def task():
resp = requests.get("some_url")
# save to mongodb

app.run()

任务是 IO 绑定(bind)的,不需要返回值。请求不会经常来,我猜最多10/s。

我测试了它,它起作用了。我想知道的是我是否可以通过这种方式使用多线程来性能提升。 Flask 会以某种方式阻止任务吗?

最佳答案

这取决于比 Flask 更多的因素,比如你在 Flask 前面使用的是什么(gunicorn、gevent、uwsgi、nginx 等)。如果你发现你对“some_url”的请求确实是一个瓶颈,将它推到另一个线程可能会提供一个提升,但这同样取决于你的个人情况; Web 堆栈中的许多元素会使该过程“变慢”。

与其在 Flask 进程上使用多线程(这很快就会变得复杂),将阻塞 I/O 推送到辅助进程可能是更好的解决方案。您可以将 Redis 消息发送到在 asyncio 事件循环上运行的进程,这将很好地扩展。

应用.py

from flask import Flask
import redis

r = redis.StrictRedis(host='127.0.0.1', port=6379)
app = Flask(__name__)

@app.route("/")
def hello():
# send your message to the other process with redis
r.publish('some-channel', 'some data')
return "OK"

if __name__ == '__main__':
app.run(port=4000, debug=True)

助手.py

import asyncio
import asyncio_redis
import aiohttp

@asyncio.coroutine
def get_page():
# get some url
req = yield from aiohttp.get('http://example.com')
data = yield from req.read()

# insert into mongo using Motor or some other async DBAPI
#yield from insert_into_database(data)

@asyncio.coroutine
def run():
# Create connection
connection = yield from asyncio_redis.Connection.create(host='127.0.0.1', port=6379)

# Create subscriber.
subscriber = yield from connection.start_subscribe()

# Subscribe to channel.
yield from subscriber.subscribe([ 'some-channel' ])

# Inside a while loop, wait for incoming events.
while True:
reply = yield from subscriber.next_published()
print('Received: ', repr(reply.value), 'on channel', reply.channel)
yield from get_page()

# When finished, close the connection.
connection.close()

if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run())

关于python - 将 concurrent.futures 与 Flask 结合使用会提高性能吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34951614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com