首页 > 解决方案 > 带 Telepot 的 Klling 线程

问题描述

我想在我的主线程上使用另一个线程。主要的一个是电报机器人api,第二个是opencv文件。我尝试了所有就绪的多处理和子进程,但由于机器人运行的线程库,它无法工作。毕竟我设法建立了“子线程”,但它不会退出“子线程”。它只工作一次,就是这样。我没有找到有关如何使用电报机器人的其他命令退出“子线程”的任何有用信息,例如:

elif telegramText == '/exit_thread':
        if my_thread.isalive():
           my_thead.exit()/my_thread.end()
           bot.sendMessage(chat_id, str("the thread isnt working, re-enter the /enb command for the new open cv thread"))

将线程创建为数字 1:

elif telegramText == '/enb':
        numbers = [1]
        for number in numbers:
            my_thread = Thread(target=doubler, args=(number, ))
            my_thread.start()

定义线程需要做什么:

def doubler(number):
if number==1:
    #print(threading.currentThread().getName() + '\n')
    bot.sendMessage(chat_id, str("Its on, Youre safe!"))
    if full_body_detec.body_detec():
        bot.sendMessage(chat_id, str("I saw some-one!! see it your self"))
        bot.sendPhoto(chat_id, photo=open('intruder.jpg', 'rb'))
        bot.sendVideo(chat_id = chat_id,video = open('output2.mp4',mode ='rb'))

我真的被困住了,伙计们会很感激你的帮助。

标签: pythonpython-3.xmultithreadingpython-multithreadingpython-telegram-bot

解决方案


使用threading.Thread.join方法my_thread.join(),但不使用.exit().end()

PS 注意拼写错误,我看到您使用的 2 个变量:my_thead 和my_th读取


推荐阅读