I’m trying to make a Telegram bot that parses GTFS data and returns the user the status of subway trains. I’m using the python-telegram-bot library and, so far, the bot I created work as intended.
However, I wrote some very pandas rich functions that process pandas dataframes to obtain the train schedule. I’ve only just realized that these functions work normally if you have a single user request, otherwise, since they are not async, they are serialized and block subsequent requests (correct me if I’m wrong).
I’ve read that asyncio might be apt to it, but I can’t make it work. What can I do in practice to async those functions?
2
Answers
You can wrap the sync function into an async function. Something like:
This function takes as parameter a sync function and returns a corresponding async function.
If you are working with v13 of PTB (python-telegram-bot) library you can pass run_async=True in your handlers, that will made your handlers run asynchrounously allowing multiple updates to be processed at same time.
Alternatively PTB is shifting towards async, you can use its alpha release which is fully asynchronous, pretty much stable and is future of PTB!
See Concurrency in PTB if you are going to use v20