gpt4 book ai didi

Python 3.5 aiohttp 即使在使用 async/await 时也会阻塞

转载 作者:行者123 更新时间:2023-11-28 17:23:45 24 4
gpt4 key购买 nike

我正在运行测试 aiohttp 网络服务器:

#!/usr/bin/env python3

from aiohttp import web
import time
import asyncio
import random
import string
import logging

logger = logging.getLogger('webserver')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

def randomword(length):
return ''.join(random.choice(string.ascii_lowercase) for i in range(length))

async def hello(request):
sleeptime = 10 # seconds

requestid = randomword(5)
logger.info("Request id: {} received - will sleep for {}".format(requestid, sleeptime))

await asyncio.sleep(sleeptime)

logger.info("Request id: {} wakeup and respond".format(requestid))
return web.json_response()

# create app and register with request handler
app = web.Application()
app.router.add_get('/', hello)
web.run_app(app, port='15000')

这有望同时处理多个传入请求,而不必等待一个请求完成后再处理下一个请求。然而,它并没有这样做——处理是顺序的(注意第 2/3 个请求的开始时间是在前一个请求完成之后):

======== Running on http://0.0.0.0:15000/ ========
(Press CTRL+C to quit)
2016-10-23 02:28:23,911 - webserver - INFO - Request id: hpzdo received - will sleep for 10
2016-10-23 02:28:33,922 - webserver - INFO - Request id: hpzdo wakeup and respond
2016-10-23 02:28:33,947 - webserver - INFO - Request id: pdfbs received - will sleep for 10
2016-10-23 02:28:43,957 - webserver - INFO - Request id: pdfbs wakeup and respond
2016-10-23 02:28:43,971 - webserver - INFO - Request id: dmkav received - will sleep for 10
2016-10-23 02:28:53,982 - webserver - INFO - Request id: dmkav wakeup and respond

我希望每个请求的开始时间几乎相同(取决于我提交它们的速度;这里我在一两秒内手动提交)。

最佳答案

您的测试技术有问题。

针对您的服务器运行 wrk 工具会提供不同的结果。

运行命令:

wrk http://127.0.0.1:15000/  

服务器输出:

======== Running on http://0.0.0.0:15000 ========
(Press CTRL+C to quit)
2016-10-23 14:58:56,447 - webserver - INFO - Request id: hkkrp received - will sleep for 10
2016-10-23 14:58:56,447 - webserver - INFO - Request id: cysoy received - will sleep for 10
2016-10-23 14:58:56,448 - webserver - INFO - Request id: udwxe received - will sleep for 10
2016-10-23 14:58:56,448 - webserver - INFO - Request id: moqkf received - will sleep for 10
2016-10-23 14:58:56,449 - webserver - INFO - Request id: shetp received - will sleep for 10
2016-10-23 14:58:56,450 - webserver - INFO - Request id: fhilc received - will sleep for 10
2016-10-23 14:58:56,451 - webserver - INFO - Request id: birax received - will sleep for 10
2016-10-23 14:58:56,452 - webserver - INFO - Request id: yyeoc received - will sleep for 10
2016-10-23 14:58:56,453 - webserver - INFO - Request id: mpkwf received - will sleep for 10
2016-10-23 14:58:56,454 - webserver - INFO - Request id: nkxiz received - will sleep for 10
2016-10-23 14:59:06,450 - webserver - INFO - Request id: hkkrp wakeup and respond
2016-10-23 14:59:06,451 - webserver - INFO - Request id: cysoy wakeup and respond
2016-10-23 14:59:06,451 - webserver - INFO - Request id: udwxe wakeup and respond
2016-10-23 14:59:06,452 - webserver - INFO - Request id: moqkf wakeup and respond
2016-10-23 14:59:06,452 - webserver - INFO - Request id: shetp wakeup and respond
2016-10-23 14:59:06,453 - webserver - INFO - Request id: fmpmm received - will sleep for 10
2016-10-23 14:59:06,453 - webserver - INFO - Request id: uusnt received - will sleep for 10
2016-10-23 14:59:06,453 - webserver - INFO - Request id: kjlzk received - will sleep for 10
2016-10-23 14:59:06,453 - webserver - INFO - Request id: elhje received - will sleep for 10
2016-10-23 14:59:06,453 - webserver - INFO - Request id: bkvjp received - will sleep for 10
2016-10-23 14:59:06,454 - webserver - INFO - Request id: fhilc wakeup and respond
2016-10-23 14:59:06,454 - webserver - INFO - Request id: birax wakeup and respond
2016-10-23 14:59:06,454 - webserver - INFO - Request id: yyeoc wakeup and respond
2016-10-23 14:59:06,455 - webserver - INFO - Request id: nyhba received - will sleep for 10
2016-10-23 14:59:06,455 - webserver - INFO - Request id: iqjvr received - will sleep for 10
2016-10-23 14:59:06,455 - webserver - INFO - Request id: osfsg received - will sleep for 10
2016-10-23 14:59:06,455 - webserver - INFO - Request id: mpkwf wakeup and respond
2016-10-23 14:59:06,456 - webserver - INFO - Request id: nkxiz wakeup and respond
2016-10-23 14:59:06,456 - webserver - INFO - Request id: ebzai received - will sleep for 10
2016-10-23 14:59:06,456 - webserver - INFO - Request id: erfhe received - will sleep for 10

关于Python 3.5 aiohttp 即使在使用 async/await 时也会阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40195734/

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