Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close pending loops properly in asyncio #825

Closed
Jinqad opened this issue May 30, 2018 · 6 comments
Closed

Close pending loops properly in asyncio #825

Jinqad opened this issue May 30, 2018 · 6 comments

Comments

@Jinqad
Copy link

Jinqad commented May 30, 2018

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?

@Lonami Lonami changed the title Asyncio Version Of Telethon Close pending loops properly in asyncio May 30, 2018
@Jinqad
Copy link
Author

Jinqad commented May 30, 2018

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!

@Lonami
Copy link
Member

Lonami commented May 30, 2018

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.

@andr-04
Copy link
Contributor

andr-04 commented Jun 6, 2018

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.

@Lonami Lonami mentioned this issue Jun 11, 2018
5 tasks
@Lonami
Copy link
Member

Lonami commented Jun 14, 2018

bb3a564 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.

@Lonami Lonami closed this as completed Jun 14, 2018
@Jinqad
Copy link
Author

Jinqad commented Jun 18, 2018

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.

@Lonami
Copy link
Member

Lonami commented Jun 18, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants