Python 非轮询/非阻塞计时器?

Python 非轮询/非阻塞计时器?,python,timer,Python,Timer,到目前为止,我找到的最佳解决方案是只使用sleep()函数。当计时器过期时,我想运行自己的回调函数。是否有任何事件驱动的方式来实现这一点 from time import sleep # Sleep for a minute time.sleep(60) 有时候,一个简单的解决方案是最好的,即使它能及时地找到答案。我曾经用它取得了巨大的成功-如果你的线程没有停止,它不会阻塞 我想我最简单的方法是检查时间,因为这比制定单独的线程解决方案简单得多,而且节约资源: def event_minute

到目前为止,我找到的最佳解决方案是只使用
sleep()
函数。当计时器过期时,我想运行自己的回调函数。是否有任何事件驱动的方式来实现这一点

from time import sleep

# Sleep for a minute
time.sleep(60)

有时候,一个简单的解决方案是最好的,即使它能及时地找到答案。我曾经用它取得了巨大的成功-如果你的线程没有停止,它不会阻塞

我想我最简单的方法是检查时间,因为这比制定单独的线程解决方案简单得多,而且节约资源:

def event_minute_later(event):
    print(time.time()) # use for testing, comment out or delete for production
    return event + 60 < time.time()

有一个内置的简单解决方案,使用模块:


您还可以将args和kwargs传递给回调函数。看。

我想这可能非常简单。看看这个例子。它甚至可以在python控制台中工作

from threading import Thread
from time import sleep

# Function to be called when the timer expires
def myFunction():
    print 'Did anyone call me?'

# Function with the timer
def myTimer(seconds):
    sleep(seconds)
    myFunction()

# Thread that will sleep in background and call your function
# when the timer expires.
myThread = Thread(target=myTimer, args=(4,))
myThread.start()
投入您想要的秒数,并继续使用控制台或运行主线程/程序。您会注意到,该函数将在计时器结束时调用

编辑

另一个很好的例子是@tarabyte的注释,其中函数的调用仅取决于某个变量或标志的值。我希望这就是@tarabyte所寻找的答案

from threading import Thread
from time import sleep

myFlag = False

# Function to be called when the flag turns on
def myFunction():
    print 'Did anyone call me?'

def myTimer():
    global myFlag
    while True:
        if myFlag:
            myFunction()
            myFlag = False
        else:
            sleep(1)

# Thread that will sleep in background and call your function
# when the myFlag turns to be True
myThread = Thread(target=myTimer)
myThread.start()

# Then, you can do whatever you want and later change the value of myFlag.
# Take a look at the output inside ipython when the value of myFlag is changed.


In [35]: myFlag
Out[35]: False

In [36]: myFlag = True

In [37]: Did anyone call me?

有几个事件/队列计时器库。我使用Twisted的反应堆,但那是因为我需要Twisted做其他事情。如果你的应用程序还有其他事情要做,你肯定不想休眠你的线程。这个问题对你有答案吗?如果是,你会接受其中一个张贴的答案吗?如果没有,你有自己的答案吗?不过我似乎无法重置计时器。我不想让这一切继续下去。我想在另一个事件上启动它,然后获取过期事件。当我尝试timer.cancel()时,我得到:raise RUNTIMERROR(“线程只能启动一次”)RUNTIMERROR:线程只能启动一次您不能/不需要重置计时器。它只工作一次。很好的例子,但我应该澄清我对重复出现的过期不感兴趣,只对我想要的过期感兴趣。开始()自己,然后在第一次尝试后结束。我想我可以通过将myTimer的内容包装在if(其他地方的一些变量集)Hi@tarabyte中来破解一个解决方案。请看我文章的编辑版本。我认为这或多或少是你想要做的。您可以修改
myFlag
的类型和可能的值,以便根据需要进行调整。如果您同意我的意见,请接受正确的答案并将其标记。@Javier“它甚至可以在python控制台中工作”-所有python代码在控制台中的工作方式与在脚本中的工作方式相同。只有一些代码只能在控制台中工作,而不能在python脚本中工作。例如,控制台中的
somevar
打印somevar的值(如果以前定义过),但在python脚本中相同的值不起任何作用。
from threading import Thread
from time import sleep

# Function to be called when the timer expires
def myFunction():
    print 'Did anyone call me?'

# Function with the timer
def myTimer(seconds):
    sleep(seconds)
    myFunction()

# Thread that will sleep in background and call your function
# when the timer expires.
myThread = Thread(target=myTimer, args=(4,))
myThread.start()
from threading import Thread
from time import sleep

myFlag = False

# Function to be called when the flag turns on
def myFunction():
    print 'Did anyone call me?'

def myTimer():
    global myFlag
    while True:
        if myFlag:
            myFunction()
            myFlag = False
        else:
            sleep(1)

# Thread that will sleep in background and call your function
# when the myFlag turns to be True
myThread = Thread(target=myTimer)
myThread.start()

# Then, you can do whatever you want and later change the value of myFlag.
# Take a look at the output inside ipython when the value of myFlag is changed.


In [35]: myFlag
Out[35]: False

In [36]: myFlag = True

In [37]: Did anyone call me?