本文整理汇总了Python中threading.Event.cancel方法的典型用法代码示例。如果您正苦于以下问题:Python Event.cancel方法的具体用法?Python Event.cancel怎么用?Python Event.cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Event
的用法示例。
在下文中一共展示了Event.cancel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FileSender
# 需要导入模块: from threading import Event [as 别名]
# 或者: from threading.Event import cancel [as 别名]
class FileSender (Thread):
def __init__ (self, coords, spool, hosted, mimetp):
Thread.__init__ (self)
self.setDaemon (False)
self.coords = coords
self.spool = spool
self.hosted = hosted
self.mimetp = mimetp
self.looping = True
self.queue = { }
self.timer = Event ()
self.readqueue ()
#DEBUG# print 'Queue:', self.queue
def finish (self):
self.looping = False
self.wakeup ()
def run (self):
def nop ():
pass
#DEBUG# print self.coords
while self.looping:
if self.timer.isSet ():
self.timer.clear ()
self.readqueue ()
pause = self.runqueue () - time ()
pause = max (1, min (pause, 3600))
print 'Sleeping for', pause, 'seconds'
self.timer.wait (pause)
def wakeup (self):
"""During most of its active life, the FileSender thread is
sleeping. If changes are made to the local filesystem
however, it may need to be notified through a call to
this function. Its response will be to reload the queue
and recalculate processing times, so this would be a
suitable response after any change, even after a mere
touch of a file that needs immediate attention.
"""
self.timer.set ()
try:
self.timer.cancel ()
except:
pass
def nextacttime (self, path):
now = time ()
try:
chg = stat (path).st_mtime
nxt = now + max (1, now - chg)
return nxt
except:
# There is a problem. Perhaps the file has gone.
# Examine quickly what the problem is. Just don't
# run wild, so wait 1 second before doing so.
return now + 1
def readqueue (self):
for indom in listdir (self.spool):
for inusr in listdir (self.spool + slash + indom):
for inuid in listdir (self.spool + slash + indom + slash + inusr):
qit = self.spool + slash + indom + slash + inusr + slash + inuid + self.mimetp + slash
try:
nxt = self.nextacttime (qit)
if nxt < self.queue [qit]:
self.queue [qit] = nxt
except:
# Exception causes include
# parallel processes removing
# this file while working it
self.queue [qit] = 0
def runqueue (self):
now = time ()
timer = now + 3600
#DEBUG# print 'RUN QUEUE WITH', len (self.queue.keys ()), 'KEYS'
for path in self.queue.keys ():
#DEBUG# print 'Looking into', path
try:
if self.queue [path] <= now:
del self.queue [path]
self.submit (path)
if self.queue.has_key (path) and self.queue [path] < timer:
timer = self.queue [path]
#DEBUG# print 'Lowered timer to', timer - time ()
except:
# Something wrong, perhaps changes to the
# queue. Ignore and continue.
pass
#DEBUG# print 'RAN QUEUE AND DONE FOR', timer - time (), 'SECONDS'
return timer
def submit (self, path):
try:
#DEBUG# print 'Would now like to submit ' + path #+ ':\n' + str (cal)
rdom = path.split (slash) [-3]
hostports = self.lookup_hostports (rdom)
been_connected = False
#.........这里部分代码省略.........