Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to work with simultaneity with telegram bot python

I dont know how to do multiprocess with bot in telegram with python. I make one thread but if this thread dont finish the bot cant answer messages.

horaPurga= now.replace(hour=23, minute=36,second=59,microsecond=0)

def purga(threading.Thread):
    now = datetime.now()

    if now >= horaPurga :
        bot.send_message(cid, 'pole')

def run():
    while True:
        purga.start()
        time.sleep(2)
like image 937
Batichico Avatar asked Sep 09 '26 10:09

Batichico


1 Answers

Try decorating your functions with @run_async:

from telegram.ext.dispatcher import run_async

@run_async
def purga():
    now = datetime.now()

Read more about it here:
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Performance-Optimizations

TLDR:

this does not actually make your code run faster. The real advantage is that I/O operations like network communication (eg. sending a message to a user) or reading/writing on your hard drive can run concurrently.

like image 162
Evyatar Meged Avatar answered Sep 12 '26 00:09

Evyatar Meged