0

在一个文档中,他们将事件循环传递给 quart。需要在路由处理程序中调用异步方法。

如何将此更改为heroku的命令行?

if __name__ == '__main__':
    loop=asyncio.get_event_loop()
    app.run(loop=loop)

我试过了

web: hypercorn  -b 0.0.0.0:${PORT} --workers=1 telegram:app -k asyncio

但还是得到了

2019-06-22 10:00:45.047703 app[web.1]:  Task <Task pending coro=<ASGIWebsocketConnection.handle_websocket() running at /app/.heroku/python/lib/python3.7/site-packages/quart/asgi.py:135> cb=[_wait.<locals>._on_completion() at /app/.heroku/python/lib/python3.7/asyncio/tasks.py:440]> got Future <Future pending> attached to a different loop
2019-06-22 10:00:45.048350 app[web.1]:  Traceback (most recent call last):
2019-06-22 10:00:45.048395 app[web.1]:  File "/app/tele.py", line 34, in create_contact
2019-06-22 10:00:45.048399 app[web.1]:  contacts =await client2(functions.contacts.ImportContactsRequest([contact]))
2019-06-22 10:00:45.048410 app[web.1]:  File "/app/.heroku/python/lib/python3.7/site-packages/telethon/client/users.py", line 60, in __call__
2019-06-22 10:00:45.048414 app[web.1]:  result = await future
2019-06-22 10:00:45.048458 app[web.1]:  RuntimeError: Task <Task pending coro=<ASGIWebsocketConnection.handle_websocket() running at /app/.heroku/python/lib/python3.7/site-packages/quart/asgi.py:135> cb=[_wait.<locals>._on_completion() at /app/.heroku/python/lib/python3.7/asyncio/tasks.py:440]> got Future <Future pending> attached to a different loop

对此的跟进问题


How to get an event loop from Quart here 说 quart 的 app.run() 使用由 asyncio 为主线程创建的默认事件循环 那么他们为什么要通过循环呢?

4

2 回答 2

2

因为hypercorn不会进去if __main__,所以需要预先创建循环并将其设置为默认值:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# do whatever you want, e.g.: 
# loop.create_task(background())

if __name__ == '__main__':
    loop = asyncio.get_event_loop()  # now unnecessary
    app.run(loop=loop)

# should be just
if __name__ == '__main__':
    app.run()
于 2019-06-27T12:53:43.357 回答
1

在before_serving中打开客户端解决了这个问题。

于 2019-07-12T13:11:07.580 回答