PTB goes asyncio in v20! #2351
Replies: 3 comments 7 replies
-
When PTB 14 is released, we should change our code to async style or it will optional to switch ? For example Rust's Reqwests library has support for both 🤔 |
Beta Was this translation helpful? Give feedback.
-
TL;DR please consider a way to process handlers of an update in different groups synchronously like now (without run_async), but process updates asynchronously between other updates. Please consider to add a way to process the group handlers synchronously during an update (like if they were a single function and like they now work without run_async), while handling updates asynchronously between them. this because a developer could not mind that updates are processed one by one, but he may be interested that group handlers are processed synchronously because you may have a group that add the users to the database processing some data, and in the second group you may send a message to that user fetching the data from database added by the previous handler. Basically processing group handlers synchronously it’s like now it works without adding the run_async params to handlers. Thank you for the great job you do ❤️ |
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
-
We are embracing the future:
asyncio
support is on its way and it's gonna be legen, wait for it, -dary!For those of you who don't know what
asyncio
does: When calling e.g.bot.send_message(…)
, your code spends a lot of time waiting for the Telegram API to respond. During that waiting time, other code could be running. So far this was achieved in PTB using multi threading. But:By now
asyncio
is a well established standard for concurrency in networking libraries. It can be faster than threaded code and provides some more flexibility. Until the beginning of 2020, PTB still supported Python 2.7, but by now a year has passed and we see an increasing amount of questions on how to integrateasyncio
code with PTB. On top of that, we have some plans for features that would heavily benefit from a switch toasyncio
.That is why with the release of version 20,
python-telegram-bot
will goasyncio
!We are aware that this will be a major breaking change, as
def callback …
toasync def callback
,message = bot.send_message(…)
tomessage = await bot.send_message(…)
) andpython-telegram-bot
in a multi-threaded manner will have to be reworkedHowever, this switch will also mean a lot of work on the internals of our library and trying to make the change backwards compatible would mean a huge additional effort. We will therefore not provide a compatibility layer. Making this decision was not easy for us, but we hope that it enables us to put our energy in further improvements and enhancements of
python-telegram-bot
rather than maintaining legacy code for backward of compatibility.This is why we're making this announcement that early in our development process: We don't know when we will release version 20. It may be in a few months or in 2 years - as you know we don't like to give ETAs. But it's important to us to involve the community in the transition and give you a chance to prepare. We will make another announcement when we have an
asyncio
branch ready. We will give you the opportunity to try the new PTB out and will try to support community based compatibility tools. The actual release will happen later and will be announced beforehand as well.We hope that this switch is in your interest. If you have questions or feedback about the announcement, feel free to comment on this GitHub thread. We will try our best to answer your questions and take your comments into account.
While we prepared the switch to
asyncio
, we took the chance and did some much needed spring cleaning in the library. While this means a more consistent user appearance, it also means a lot of breaking changes. We will write a transition guide, as always.As of now, all development to the main branch has been stopped. If an API update happens or some mayor bug appears, we will release another version, otherwise the next proper release will be V20.
Your PTB Developer Team
»Dread it. Run from it. asyncio still arrives.« - Thanos, ca. 2018, allegedly
Beta Was this translation helpful? Give feedback.
All reactions