Telethon: Close pending loops properly in asyncio

Created on 30 May 2018  路  6Comments  路  Source: LonamiWebs/Telethon

Can you close the event loop properly?
Or how do i access the loop which its on? So i can do it properly
because right now when i do
CTRL+C
It throws

task: <Task pending coro=<TelegramBareClient._recv_loop_impl() running at /usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py:660> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fde255a1f18>()]>>
05-30 03:01 asyncio      ERROR    Task was destroyed but it is pending!
task: <Task pending coro=<TelegramBareClient._ping_loop_impl() running at /usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py:630> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fde255a1af8>()]>>
05-30 03:01 asyncio      ERROR    Task was destroyed but it is pending!
task: <Task pending coro=<TelegramBareClient._state_loop_impl() running at /usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py:635> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fde255a1528>()]>>
05-30 03:01 asyncio      ERROR    Task was destroyed but it is pending!
task: <Task pending coro=<TelegramBareClient._recv_loop_impl() running at /usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py:660> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fde255a1e88>()]>>
05-30 03:01 asyncio      ERROR    Task was destroyed but it is pending!
task: <Task pending coro=<TelegramBareClient._ping_loop_impl() running at /usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py:630> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fde255a1c18>()]>>
05-30 03:01 asyncio      ERROR    Task was destroyed but it is pending!
task: <Task pending coro=<TelegramBareClient._state_loop_impl() running at /usr/local/lib/python3.6/dist-packages/telethon/telegram_bare_client.py:635> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fde243740a8>()]>>

Could you advise me?

Most helpful comment

The loop can be shared with other tasks, not only Telethon. So opening and closing the loop is not under control of Telethon.

My general usecase is the following:

loop = asyncio.get_event_loop()
main = asyncio.ensure_future(main(loop), loop=loop)
for signame in ('SIGINT', 'SIGTERM'):
    loop.add_signal_handler(getattr(signal, signame), loop.stop)
loop.run_forever()  # at this point you are waiting the loop stops
main.cancel()
loop.run_until_complete(main)
pending = asyncio.Task.all_tasks(loop)
for task in pending:
    task.cancel()
loop.run_until_complete(asyncio.gather(*pending, loop=loop))
loop.close()

Right now probably _recv_loop_impl is not ready for asyncio.CancelledError but it will be done.

All 6 comments

Is there a way i can access the loop? and try do it myself because i don't know when you're going to do it and i just wanted to have that in my app so if you could help thanks. your lib rocks!

Thanks, yes in Python you can access everything, client._recv_loop will effectively work. You can also subclass it and use your own class and do the modifications you need, but if you do fix it, please make a pull request.

The loop can be shared with other tasks, not only Telethon. So opening and closing the loop is not under control of Telethon.

My general usecase is the following:

loop = asyncio.get_event_loop()
main = asyncio.ensure_future(main(loop), loop=loop)
for signame in ('SIGINT', 'SIGTERM'):
    loop.add_signal_handler(getattr(signal, signame), loop.stop)
loop.run_forever()  # at this point you are waiting the loop stops
main.cancel()
loop.run_until_complete(main)
pending = asyncio.Task.all_tasks(loop)
for task in pending:
    task.cancel()
loop.run_until_complete(asyncio.gather(*pending, loop=loop))
loop.close()

Right now probably _recv_loop_impl is not ready for asyncio.CancelledError but it will be done.

https://github.com/LonamiWebs/Telethon/commit/bb3a5645008b01ee18f5adf1554ea7ce8a7cb15a has been merged, reopen after the next version is published if this error still occurs. Note that master is now running under asyncio so your code won't be compatible with it. To preserve this compatibility, a new branch emulating synchronous behaviour will be created.

Asyncio is merged with Master? So i just do pip install telethon now instead of telethon-aio if its merged with master? Sorry not familiar with such terms.

PyPi and GitHub are unrelated, and the telethon and telethon-aio packages will remain being what they are now (but with said rewrite). pip install --upgrade git+https://github.com/LonamiWebs/Telethon@master will now, however, install the asyncio version indeed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fleyderer picture Fleyderer  路  3Comments

justxor picture justxor  路  4Comments

ghost picture ghost  路  4Comments

saharokk picture saharokk  路  3Comments

aminhyper picture aminhyper  路  3Comments