我能用古玩和三井吗。为什么不呢?

2024-05-17 03:19:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python 3.6上的sanic应用程序中使用async raw socket 并且对来自curio的异步函数在sanic异步上下文中不起作用感到非常惊讶。最小概念证明:

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请求上生成核心转储

^{pr2}$

Tags: 函数textfromimportapp应用程序curioasync
1条回答
网友
1楼 · 发布于 2024-05-17 03:19:33

为了将这个问题与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.

相关问题 更多 >