from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import time
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("start")
time.sleep(10) # a process that's going to take some time
await update.message.reply_text("finish")
app = ApplicationBuilder().token("TOKEN HERE").build()
app.add_handler(CommandHandler("start", start))
app.run_polling()
This is the simplest example of the problem i'm currently facing in a project
The bot has to do a process that takes some time to finish
But the bot stops responding to other users during that process
I tried everything
I tried older versions of python telegram bot
I tried using threads (which won't work on async functions) and asyncio (i'm not so familiar with this sorta stuffs but for some reasons still did not respond to other users)
I even tried creating two functions inside the "start" function (one async and one not async) and then running the async function through a thread of the normal function.
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import time
import threading
import asyncio
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
async def thread1(upd: Update, ctx: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('start')
time.sleep(10)
await update.message.reply_text('finish')
def thread_the_thread(upd: Update, ctx: ContextTypes.DEFAULT_TYPE):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(thread1(upd, ctx))
loop.close()
t = threading.Thread(target=thread_the_thread, args=(update, context))
t.start()
app = ApplicationBuilder().token("TOKEN HERE").build()
app.add_handler(CommandHandler("start", start))
app.run_polling()
But when i used the bot with two different users...
telegram.error.NetworkError: Unknown error in HTTP implementation: RuntimeError('<asyncio.locks.Event object at 0x0000022361E0A920 [unset]> is bound to a different event loop')