gpt4 book ai didi

python - 我可以将 curio 与 sanic 一起使用吗?为什么不?

转载 作者:行者123 更新时间:2023-12-01 03:05:41 28 4
gpt4 key购买 nike

我尝试在 Python 3.6 上的 sanic 应用程序中使用异步原始套接字令我们感到非常惊讶的是,curio 中的异步函数无法在正常的异步上下文中工作。最小概念证明:

import curio
from sanic import Sanic
from sanic.response import text
app = Sanic(__name__)


@app.route('/')
async def index(request):
sock = await curio.open_connection("google.com", 443, ssl=True)
resp = await sock.recv(1024)
return text(resp)


app.run(host="0.0.0.0", port=5001)

此代码在任何 http 请求上生成核心转储

$ python3 test2.py 
2017-04-17 17:33:13,266: INFO: Goin' Fast @ http://0.0.0.0:5001
2017-04-17 17:33:13,270: INFO: Starting worker [93060]
Fatal Python error: GC object already tracked

Current thread 0x00007fc57706e480 (most recent call first):
File "/usr/lib/python3.6/linecache.py", line 165 in lazycache
File "/usr/lib/python3.6/traceback.py", line 345 in extract
File "/usr/lib/python3.6/traceback.py", line 497 in __init__
File "/usr/lib/python3.6/traceback.py", line 117 in format_exception
File "/usr/lib/python3.6/traceback.py", line 163 in format_exc
File "/usr/lib/python3.6/site-packages/sanic/handlers.py", line 93 in default
File "/usr/lib/python3.6/site-packages/sanic/handlers.py", line 70 in response
File "/usr/lib/python3.6/site-packages/sanic/app.py", line 472 in handle_request
File "/usr/lib/python3.6/site-packages/sanic/server.py", line 410 in serve
File "/usr/lib/python3.6/site-packages/sanic/app.py", line 561 in run
File "test2.py", line 14 in <module>
Aborted (core dumped)

最佳答案

为了将此问题与 github 问题联系起来,OP 已经提出了。

Using curio in sanic controller .

总结(来自问题评论):

From the official Curio docs: "Curio can also submit work to the asyncio event loop with the provision that it must be running separately in a different thread."

Look at this issue

Curio's main implementation of events is a queue, not an event loop. You're trying to spawn tasks inside the same thread Sanic (and the event loop) are running from.

关于python - 我可以将 curio 与 sanic 一起使用吗?为什么不?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43456508/

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