我试图在sanicPython 3.6应用程序中使用异步原始套接字
真的很惊讶,来自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请求上生成核心转储

$ 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

总结(来自问题评论):


  来自官方的Curio文档:
  “ Curio还可以将工作提交到asyncio事件循环,条件是它必须在不同的线程中单独运行。”
  
  Look at this issue
  
  Curio事件的主要实现是队列,而不是事件循环。您正试图在运行Sanic的同一线程(和事件循环)中生成任务。

10-06 13:00