如何与电报bot python同时工作

时间:2017-11-28 20:35:25

标签: python multithreading telegram simultaneous

我不知道如何使用python在电报中使用bot进行多进程。我创建一个线程,但如果这个线程没有完成,机器人无法回复消息。

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)

1 个答案:

答案 0 :(得分:0)

尝试使用@run_async来装饰你的功能:

from telegram.ext.dispatcher import run_async

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

在此处阅读更多相关信息:
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Performance-Optimizations

TLDR:

  

这实际上并没有使您的代码运行得更快。真正的优势是网络通信等I / O操作(例如向用户发送消息)或硬盘驱动器上的读/写操作可以同时运行。