9

我正在尝试使用工具在我的本地机器上进行 HTTP 基准测试,该工具适用于我在+autocannon上构建的全新的项目设置。FastAPIuvicorn

但是在某些时候或随机(我不知道发生了什么)我遇到了一个 ASGI 异常。

[2019-11-14 14:16:40 +0100] [98098] [ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/fastapi/applications.py", line 139, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/applications.py", line 134, in __call__
    await self.error_middleware(scope, receive, send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/middleware/errors.py", line 178, in __call__
    raise exc from None
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/middleware/errors.py", line 156, in __call__
    await self.app(scope, receive, _send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/routing.py", line 590, in __call__
    await route(scope, receive, send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/routing.py", line 208, in __call__
    await self.app(scope, receive, send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/routing.py", line 44, in app
    await response(scope, receive, send)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/responses.py", line 125, in __call__
    "headers": self.raw_headers,
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/exceptions.py", line 59, in sender
    await send(message)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/middleware/errors.py", line 153, in _send
    await send(message)
  File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 482, in send
    self.transport.write(b"".join(content))
  File "uvloop/handles/stream.pyx", line 673, in uvloop.loop.UVStream.write
  File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x7fb255569780>; the handler is closed

要启动我的应用程序,我会:

gunicorn url_shortener:app -w 1 -k uvicorn.workers.UvicornWorker

我试图增加工人的数量。我仍然得到这些例外。

为了进行基准测试,我这样做:

autocannon -c 100 -d 40 -p 10 localhost:8000/shorten -H "Content-Type: application/json" -m POST -b '{"url": "http://yigitgenc.com"}'

PS:我的数据库(PostgreSQL)在Docker容器上工作。而且我还进行异步数据库交互(当然)。也许添加此信息会有所帮助。

4

0 回答 0