【问题标题】:Calling a threading.timer from a function and not seeing any output of the called thread in Python从函数调用 threading.timer,但在 Python 中看不到被调用线程的任何输出
【发布时间】:2026-02-03 21:40:01
【问题描述】:

我的想法是我正在向外部设备发送消息并设置一个标志,告诉程序我正在等待特定的响应。 在等待响应之前,程序将继续接收其他消息并评估它们以了解它们是否是我请求的响应。 为此,我使用计时器调用另一个函数,因此我在配置的超时到期后重置标志。换句话说,如果这样的函数执行并且标志仍然被标记为等待响应,这意味着没有收到响应,因此我显示一条消息,表明发生了超时。 如果响应在计时器到期之前到达,那么我取消计时器,处理响应并重置标志。

在计时器计时期间到达的任何消息都会被处理,不会出现任何问题。

我遇到的问题是第二个线程确实执行并正确结束,但我没有看到它的任何输出。 我可以通过在计时器启动后调用下面的打印机()函数来看到它正在运行。我看到 2 个线程,直到 12 秒过去,然后我再次看到 1 个线程。第二个线程应该打印一些消息(并且在完整的代码中它应该为外部应用程序生成事件)。 但我看不到它的任何印记,也看不到它应该生成的事件。 我怀疑与范围有关的东西,因为我正在从一个函数创建一个线程来执行另一个函数。我真的很想要这个。

为简单起见,我删除了部分代码,但程序基本上是这样工作的: 当从串口接收到任何数据时,将调用 processData 函数。 当我想通过串口发送任何数据时,会调用 MySensorsGatewayWrite。

非常欢迎任何帮助,因为我现在陷入困境。

谢谢!

import threading
from threading import Timer,Thread,Event

global waitingforresponse
waitingforresponse=0
global t

def processData2 (received):
    global waitingforresponse
    if waitingforresponse == 1:
        waitingforresponse=0    #reset for further use
        print "Response received: "+str(received)
    else:
        print "Normal message that will be processed"

def MySensorsGatewayWrite2(sendMessage):
    global waitingforresponse
    print "Data to send: "+str(sendMessage)
    if sendMessage=="question":
        waitingforresponse = 1  #sets a flag so we can check it later
        timerstart()
    else:
        waitingforresponse = 0  #unsets a flag so we can check it later

def timerstart():
    global t
    global waitingforresponse
    t = Timer(12,checktimeout)
    t.start()
    print "Starting to count the timer for "+str(12)+" seconds"
    threadsativas=threading.activeCount()
    print "Active Threads="
    print str(threadsativas)

def checktimeout():
    global t
    global waitingforresponse
    t.cancel()
    print "Finished to count the timer for "+str(12)+" seconds"
    if waitingforresponse==1:   #if it is still 1 after the timer period (ie. flag has not been reset by receiving a response)
        print ("Didn't receive a response in time. Sensor down?")
    else:
        print ("Already received a response in time! Good!")

def printer2():
    print 'ipsem lorem'
    Activethreads=threading.activeCount()
    print "Activethreads="
    print str(Activethreads)

#simulates some message being sent and receiving a response
printer2()
MySensorsGatewayWrite2("question")
printer2()
processData2("Some response")
printer2()

#simulates some message being sent and NOT receiving a response
printer2()
MySensorsGatewayWrite2("question")
printer2()

【问题讨论】:

  • 有一个问题,使用线程,什么东西,两个问题......
  • 问题的一半是你的代码风格丑陋,所以代码本身很难阅读。再加上它几乎不是一个最小的例子,这使得这个问题很难回答。你能创建一个MCVE 并希望遵循 PEP8 吗?
  • 我已根据建议将代码编辑为更简单的版本,但现在可以使用。我需要检查我的原始代码有什么问题,并可能对此发表评论。谢谢
  • 我仍然想知道函数如何来回调用另一个线程的函数是否存在限制。此外,如果在不同线程的函数之间共享变量的方式有任何限制。

标签: python multithreading function timer


【解决方案1】:

嗯,我已经能够解决我遇到的问题。 上面原始问题中的示例(在最后一次编辑之后)和现在一样完美地工作。

原始代码(我从这个问题中删除)已被重新编码,我发现了一些错误。错误的原因是我急于让代码工作并且做得很差。

最后,希望这可以作为未来其他人的榜样。

还是谢谢。

【讨论】: