1

In a doc they passed event loop to quart. Need to call async method in route handler.

How to change this to command line for heroku?

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

I tried

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

But still got

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 

Follow up question for this


How to obtain an event loop from Quart here says quart's app.run() uses the default event loop created by asyncio for the main thread Then why they pass the loop?

6
  • Any minimal repo to reproduce the issue? Commented Jun 24, 2019 at 15:01
  • github.com/LonamiWebs/Telethon/pull/1207 with heroku Commented Jun 24, 2019 at 17:32
  • Shouldn't the last two lines be inside a if __name__ == '__main__':? Commented Jun 25, 2019 at 12:53
  • Yes , its inside Commented Jun 25, 2019 at 12:58
  • Was not in your pull request, that's why asked Commented Jun 25, 2019 at 12:59

2 Answers 2

3
+50

Because hypercorn will not go inside if __main__, you need to create loop beforehand and set it as default:

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() 
Sign up to request clarification or add additional context in comments.

2 Comments

Will hypercorn use the loop we created?
Need to call async method in route handler.
1

Opening client inside before_serving solved this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.