如何与电报机器人 python 同时工作 [英] how to work with simultaneity with telegram bot python
问题描述
我不知道如何使用 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)
推荐答案
尝试使用 @run_async 装饰您的函数:
Try decorating your functions with @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:
Read more about it here:
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Performance-Optimizations
TLDR:
这实际上并不会使您的代码运行得更快.真正的优势在于,诸如网络通信(例如,向用户发送消息)或硬盘驱动器上的读/写等 I/O 操作可以并发运行.
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.
这篇关于如何与电报机器人 python 同时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!