本文整理汇总了Python中threading.Timer.join方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.join方法的具体用法?Python Timer.join怎么用?Python Timer.join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Timer
的用法示例。
在下文中一共展示了Timer.join方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def main():
print "Main:enter", time.time()
while True:
t = Timer(5, poll_port)
t.start()
t.join()
print "after join:", time.time()
示例2: play
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def play(args):
"""Handles the 'play' command."""
from threading import Timer
from lib.configuration import Configuration
from lib.task import Task
config = utils.parse_config(args, Configuration())
tasks = []
for task, items in config.tasks.items():
t = Timer(items['timing'], Task.run_task,
args=(task, len(tasks) + 1, items, config))
t.daemon = True
t.start()
tasks.append(t)
duration = config.duration
if duration == 0:
for t in tasks:
t.join()
else:
start = time.time()
while time.time() < start + duration:
finished = True
for t in tasks:
if not t.finished.is_set():
finished = False
break
if finished:
break
time.sleep(1)
示例3: throughput
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def throughput(seconds=10,cocurrency=1):
'''
seconds should be greater than or equal to 10
1000w pv = 115 rps
'''
stop_flag=Event()
processes=[]
t=Timer(seconds,stop,args=[stop_flag])
q = Queue()
for i in range(cocurrency):
processes.append(Process(target=run,args=(q,stop_flag)))
t.start()
for p in processes:
p.start()
#print 'start waiting for workers:',len(processes)
stop_flag.wait()
for t in processes:
t.join()
total=err=cost=0
while not q.empty():
(req_counter,err_counter,time_cost)=q.get()
total=total+req_counter
err=err+err_counter
cost=cost+time_cost
cost=cost/total if total>0 else 0
return total,err,cost
示例4: run_task
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def run_task(task):
tests_run[task.name] = True
def delay_completion():
tests_completed[task.name] = True
timer = Timer(1, delay_completion)
timer.start()
timer.join()
示例5: start
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def start(self):
if self.reporter != None:
timer = Timer(1, self.reporter.start, kwargs={})
self.timer.append(timer)
timer.start()
for watcher in self.observers:
if serviceconfig.isWin32() == True:
timer = Timer(1, watcher.start, kwargs={})
else:
timer = Timer(1, watcher.startScandir, kwargs={})
self.timer.append(timer)
timer.start()
if serviceconfig.isWin32() == True:
for timer in self.timer:
timer.join()
else:
activeThreads = []
for timer in self.timer:
activeThreads.append(timer)
while len(activeThreads) > 0:
for timer in activeThreads:
timer.join(10)
activeThreads = []
for timer in self.timer:
if timer.is_alive():
activeThreads.append(timer)
示例6: TimedRunner
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
class TimedRunner():
def __init__(self, interval, func):
self.time_interval = interval
self.func = func
self.run = True
def start(self):
if self.run:
self.func()
self.thread = Timer(self.time_interval, self.start)
self.thread.start()
else:
self.thread = None
try:
while self.thread and self.thread.is_alive():
self.thread.join(5)
except KeyboardInterrupt:
self.run = False
# Hand the screen back to the terminal
curses.endwin()
# Exit thread
sys.exit()
def cancel(self):
self.thread.cancel()
示例7: button_wait
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def button_wait(b, o, press, push_time, timeout_time):
def push():
o.off()
time.sleep(MINIMUM_BUTTON_PRESS_PERIOD)
o.on()
kwargs = {}
if timeout_time:
kwargs['timeout'] = timeout_time
kwargs['press'] = press
o.on()
time.sleep(MINIMUM_BUTTON_PRESS_PERIOD)
# clear queue
if isinstance(b, list):
for button in b:
button.presses()
else:
b.presses()
try:
t = Timer(push_time, push)
t.start()
if isinstance(b, list):
wait_ret = Button.wait_many(b, **kwargs)
else:
wait_ret = b.wait(**kwargs)
finally:
t.join()
print("button wait returned: ", wait_ret)
return wait_ret
示例8: test_sqs_longpoll
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def test_sqs_longpoll(self):
c = SQSConnection()
queue_name = "test_sqs_longpoll_%s" % int(time.time())
queue = c.create_queue(queue_name)
self.addCleanup(c.delete_queue, queue, True)
messages = []
# The basic idea is to spawn a timer thread that will put something
# on the queue in 5 seconds and verify that our long polling client
# sees the message after waiting for approximately that long.
def send_message():
messages.append(queue.write(queue.new_message("this is a test message")))
t = Timer(5.0, send_message)
t.start()
self.addCleanup(t.join)
start = time.time()
response = queue.read(wait_time_seconds=10)
end = time.time()
t.join()
self.assertEqual(response.id, messages[0].id)
self.assertEqual(response.get_body(), messages[0].get_body())
# The timer thread should send the message in 5 seconds, so
# we're giving +- .5 seconds for the total time the queue
# was blocked on the read call.
self.assertTrue(4.5 <= (end - start) <= 5.5)
示例9: run
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def run(self):
while not self.stopThread:
t = Timer(5, self.kill)
t.start()
t.join()
示例10: Block
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
class Block(Play):
def __init__(self, owner, cardType):
super(Block, self).__init__(owner)
self.cardType = cardType
def startPlay(self, **kwargs):
myDict = kwargs
self.playTimer = Timer(30.0, self.playCompleted, args=None, kwargs=myDict) # Passing arguments as **kwargs
self.waitingForChallenge = True
self.playTimer.start()
def playBlocked(self, **kwargs): #In this case this play cant be blocked, only challenged
pass
def playChallenged(self, **kwargs):
print(kwargs)
print("cardType: "+str(self.cardType))
def playCompleted(self, *args, **kwargs):
game = kwargs.get('game')
bot = kwargs.get('bot')
text = GameStrings.GAME_STATUS_MESSAGE_BLOCK_SUCCESS
game.sendMessageToAllPlayers(text, bot)
text = game.changeTurn()
game.sendMessageToAllPlayers(text, bot)
def killPlayTimer(self):
self.playTimer.cancel()
self.playTimer.join() # Makes main thread stop and wait for timer to get canceled properly (maybe a bad idea since a lor of ppl will be playing in different rooms at the
# same time? Gotta see how much lag this generates)
self.playTimer = None
示例11: func
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def func(*args):
delay = int(random.random()*10)
# print("inside function with delay = ", delay)
if args:
r = Timer(delay, task, (args[0],[delay]))
else:
r = Timer(delay, task, [delay])
r.start()
r.join()
示例12: run_copy_task
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def run_copy_task(task):
def delay_completion():
src_file = open(task.src[0].path, 'rt')
dst_file = open(task.dst[0].path, 'wt')
dst_file.write(src_file.read())
dst_file.close()
src_file.close()
timer = Timer(1, delay_completion)
timer.start()
timer.join()
示例13: __init__
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
class LoggerTimer:
"""This class provides a timer with a repeat functionality based on a interval"""
def __init__(self, interval, func_name, var_name, device):
"""
interval -- the repeat interval in seconds
func -- the function which will be called
"""
self.exit_flag = False
if interval < 0:
interval = 0
self._interval = interval # in seconds
self._func_name = func_name
self._var_name = var_name
self._device = device
self._was_started = False
self._t = Timer(self._interval, self._loop)
def _loop(self):
"""Runs the <self._func_name> function every <self._interval> seconds"""
start = time.time() # FIXME: use time.monotonic() in Python 3
getattr(self._device, self._func_name)(self._var_name)
elapsed = max(time.time() - start, 0) # FIXME: use time.monotonic() in Python 3
self.cancel()
if self.exit_flag:
return
self._t = Timer(max(self._interval - elapsed, 0), self._loop)
self.start()
def start(self):
"""Starts the timer if <self._interval> is not 0 otherwise the
timer will be canceled
"""
if self._interval == 0:
self.cancel()
return
self._t.start()
self._was_started = True
def stop(self):
self.exit_flag = True
self._was_started = False
def cancel(self):
self._t.cancel()
def join(self):
if self._interval == 0: # quick fix for no timer.start()
return
if self._was_started:
self._t.join()
示例14: __init__
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
class Session:
def __init__(self, sessionURL, autoHeartbeat=True, autoHeartbeatInterval=10):
self.url = sessionURL
self.rpc = xmlrpc.client.ServerProxy(self.url)
self.connected = True
if autoHeartbeat:
self.rpc.heartbeat(autoHeartbeatInterval)
self.autoHeartbeatInterval = autoHeartbeatInterval
self.autoHeartbeatTimer = Timer(autoHeartbeatInterval-1, self.doAutoHeartbeat)
self.autoHeartbeatTimer.start()
else:
self.rpc.heartbeat(300)
def __del__(self):
self.cancelSession()
def cancelSession(self):
if self.autoHeartbeatTimer != None:
self.autoHeartbeatTimer.cancel()
self.autoHeartbeatTimer.join()
self.autoHeartbeatTimer = None
if self.connected:
self.rpc.cancelSession()
self.connected = False
def setOperatingMode(self, mode):
if mode == 0:
self.stopEdit()
elif mode == 1:
return self.startEdit()
else:
raise ValueError("Invalid operating mode")
def startEdit(self):
self.rpc.setOperatingMode(1)
self.edit = Edit(self.url + 'edit/')
return self.edit
def stopEdit(self):
self.rpc.setOperatingMode(0)
self.edit = None
def doAutoHeartbeat(self):
newHeartbeatInterval = self.rpc.heartbeat(self.autoHeartbeatInterval)
self.autoHeartbeatInterval = newHeartbeatInterval
# schedule event a little ahead of time
self.autoHeartbeatTimer = Timer(self.autoHeartbeatInterval-1, self.doAutoHeartbeat)
self.autoHeartbeatTimer.start()
def __getattr__(self, name):
# Forward otherwise undefined method calls to XMLRPC proxy
return getattr(self.rpc, name)
示例15: test_monitor_leftover_events
# 需要导入模块: from threading import Timer [as 别名]
# 或者: from threading.Timer import join [as 别名]
def test_monitor_leftover_events(client):
xfail_if_xenbus(client)
with client.monitor() as m:
m.watch(b"/foo/bar", b"boo")
def writer():
for i in range(128):
client[b"/foo/bar"] = str(i).encode()
t = Timer(.25, writer)
t.start()
m.unwatch(b"/foo/bar", b"boo")
assert not m.events.empty()
t.join()