本文整理汇总了Python中threading.Event.stop方法的典型用法代码示例。如果您正苦于以下问题:Python Event.stop方法的具体用法?Python Event.stop怎么用?Python Event.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Event
的用法示例。
在下文中一共展示了Event.stop方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: after
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import stop [as 别名]
def after(delay, action, *args):
"""
Execute an action after a given number of seconds.
This function is executed in a separate thread.
Parameters
----------
delay : float
Number of seconds to delay the action.
action
To be taken after the interval.
args : tuple, default is ()
Arguments for the action.
Returns
-------
Event
A timer object that can be terminated using the `stop()` method.
"""
event = Event()
def wait():
if event.wait(delay):
return
action(*args)
Thread(target=wait).start()
event.stop = event.set
return event
示例2: repeat
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import stop [as 别名]
def repeat(interval, action, *args):
"""
Repeat an action forever after a given number of seconds.
If a sequence of events takes longer to run than the time available
before the next event, the repeater will simply fall behind.
This function is executed in a separate thread.
Parameters
----------
interval : float
Number of seconds between executions.
action
To be taken after the interval.
args : tuple, default is ()
Arguments for the action.
Returns
-------
Event
A timer object that can be terminated using the `stop()` method.
"""
event = Event()
def loop():
starttime = time.time()
while True:
nexttime = starttime + interval
action(*args)
after_action = time.time()
if event.wait(nexttime - after_action):
break
starttime = max(after_action, nexttime)
Thread(target=loop).start()
event.stop = event.set
return event