How to run PTB with asyncio in a thread (together with a Flask web server) #3516
Unanswered
ferraridavide
asked this question in
Q&A
Replies: 1 comment 2 replies
-
You have already been pointed to the relevant wiki section in the user group: https://t.me/pythontelegrambotgroup/652977?thread=652971 😉 (As I see no mention on |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to begin by stating that I am not an expert in asyncio, but I hope that sharing my experience can be beneficial for others as well, particularly since the transition to asyncio has recently been made.
I am currently working on building a bot where users can subscribe to be notified when certain "events" occur, the script receives these requests from the bot and subscribes to a webhook for the event. Once the webhook calls back, the user receives a message.
To listen for the webhook, I am using Flask, which as far as I know, does not utilize asyncio.
The
app.run(...)
function in Flask is blocking, but I can run it in a separate thread without any issues.The
application.run_pooling()
function in PTB is also blocking and when I attempt to run it on a separate thread, I encounter an error:RuntimeError: There is no current event loop in thread.
I have tried various methods such asasyncio.run_coroutine_threadsafe
andasyncio.create_task,
but I have been unable to successfully implement them.I am wondering if the correct approach would be to put both Flask and PTB on two new threads and if so, how can I successfully run PTB on a thread? If not, what alternative solution should I explore?
Thank you for your help.
Beta Was this translation helpful? Give feedback.
All reactions