本文整理汇总了Python中util.Timer.cancel方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.cancel方法的具体用法?Python Timer.cancel怎么用?Python Timer.cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类util.Timer
的用法示例。
在下文中一共展示了Timer.cancel方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TimeoutSocketOne
# 需要导入模块: from util import Timer [as 别名]
# 或者: from util.Timer import cancel [as 别名]
class TimeoutSocketOne(common.socket):
'''
single socket timeout socket
'''
@lock
def try_connect(self, address, succ, fail, time_to_wait, provide_init):
provide_init(self)
self.real_success = succ
self.fail = fail
self.dead = False
self.data = None
#make new socket
self.make_socket()
self.timeoutvalid = True
self.timeout = Timer(time_to_wait, self.handle_timeout)
self.timeout.start()
print '*'*40
from util import funcinfo
print funcinfo(self.connect)
print '*'*40
self.connect(address, error=self.do_fail)
#do connect with callback
#success indicates that the socket started, but guarantees nothing
#error indicates that there was a problem, should try to close + do fail
def succ(self):
info('succ')
self.real_success()
@lock
def do_fail(self, *a, **k):
info('do_fail')
if self.timeout is not None:
self.timeout.cancel()
self.timeout = None
self.timeoutvalid = False
self.close()
print a, k
self.fail(*a,**k)
@lock
def handle_connect(self):
info('CONNECT')
if self.timeout is not None:
self.timeout.cancel()
self.timeout = None
self.timeoutvalid = False
self.succ()
#cancel timeout
#success
@lock
def handle_timeout(self):
info('TIMEOUT')
#mark as dead
if self.timeoutvalid:
if self.timeout is not None:
self.timeout.cancel()
self.timeout = None
self.timeoutvalid = False
self.close()
self.dead = True
self.fail()
@lock
def collect_incoming_data(self, data):
self.data += data
@lock
def __error(self):
olddead = self.dead
self.dead = True
if self.timeout is not None:
self.timeout.cancel()
self.timeout = None
self.timeoutvalid = False
#cancel timeout
self.close()
if not olddead:
self.fail()
def handle_error(self, e=None):
info('ERROR: %r', e)
import traceback;traceback.print_exc()
self.__error()
def handle_expt(self):
info('EXPT')
self.__error()
def handle_close(self):
info('CLOSE')
self.__error()
示例2: OscarTimeoutSocket
# 需要导入模块: from util import Timer [as 别名]
# 或者: from util.Timer import cancel [as 别名]
class OscarTimeoutSocket(common.socket):
def tryconnect(self, ips, on_connect, on_fail, timeout = 2.0):
self._connectedonce = False
info('tryconnect Y=%r, N=%r', on_connect, on_fail)
self.ips = self.iptuples(ips)
if not callable(on_connect) or not callable(on_fail):
raise TypeError( 'on_connect and on_fail must be callables' )
self.on_connect = on_connect
self.on_fail = on_fail
self.timetowait = timeout
self._tryagain(timeout)
def tryaccept(self, addr, on_connect, on_fail, timeout = 1.5):
self._connectedonce = False
info('tryaccept Y=%r, N=%r', on_connect, on_fail)
self.ips = ()
self.on_connect = on_connect
self.on_fail = on_fail
info('listening for a connection at %s:%d', *addr)
self.bind( addr )
self.listen(1)
if timeout:
info('timeout in %r secs', timeout)
def dotimeout():
info('TIMEOUT. calling %r', self.on_fail)
self.on_fail()
self.timeout = Timer(timeout, dotimeout)
self.timeout.start()
def _tryagain(self, timetowait):
# Try the next IP.
addr = self.ips.pop(0)
if len(self.ips) > 0:
timeoutfunc = partial(self._tryagain, timetowait)
else:
# This is the last one.
timeoutfunc = self.on_fail
self.timeout = Timer(timetowait, timeoutfunc)
info('%r attempting conn: %s:%d', self, *addr)
self.make_socket()
self.connect(addr, error=timeoutfunc)
info('timeout is %r seconds...', timetowait)
if self.timeout is not None:
self.timeout.start()
def handle_expt(self):
info('handle_expt in %r', self)
self.handle_disconnect()
def handle_error(self, e=None):
info('handle_error in %r', self)
import traceback
traceback.print_exc()
if not self._connectedonce:
self.handle_disconnect()
else:
self.close()
def handle_disconnect(self):
self.cancel_timeout()
self.close()
if len(self.ips) > 0:
info('got an error, trying next ip immediately: ' + \
repr(self.ips[0]))
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self._tryagain(self.timetowait)
elif not self._connectedonce:
info('no more ips to attempt, calling on_fail (%r)', self.on_fail)
self.on_fail()
def handle_connect(self):
info('connected!')
self.cancel_timeout()
#self.handle_disconnect = lambda: None
self._connectedonce = True
self.on_connect()
self.on_fail = Sentinel
def handle_accept(self):
self.cancel_timeout()
conn, address = self.accept()
info('%r connection accepted (%r), canceling timeout and calling %r', self, address, self.on_connect)
self._connectedonce = True
self.on_connect(conn)
def cancel_timeout(self):
# Cancel any timeout.
#.........这里部分代码省略.........
示例3: TimeoutSocket
# 需要导入模块: from util import Timer [as 别名]
# 或者: from util.Timer import cancel [as 别名]
class TimeoutSocket(common.socket):
def tryconnect(self, ips, on_connect, on_fail, timeout=2.0):
'''
Setup for a new set of ips and start the connect routine
@param ips:
@param on_connect:
@param on_fail:
@param timeout:
'''
self.cancel_timeout()
self.timetowait = timeout
self.on_connect = on_connect
self.on_fail = on_fail
self._ips = iptuples(ips)
self.attempts = 0
self._accepting = False
self.try_connect()
def try_connect(self):
'Do the connection routine.'
addr = self._ips[self.attempts]
log.warning('tryconnect: %r', (addr,))
self.attempts += 1
self.timeout = Timer(self.timetowait, lambda s=self.socket: self.handle_timeout(s))
self.make_socket()
if self.timeout is not None:
self.timeout.start()
def succ(*a, **k):
log.info("WIN")
def fail(*a, **k):
log.info("FAIL")
self.connect(addr, success=succ, error=fail)
def tryaccept(self, addr, on_connect, on_fail, timeout = 1.5):
self._accepting = True
info('tryaccept Y=%r, N=%r', on_connect, on_fail)
self.on_connect = on_connect
self.on_fail = on_fail
info('listening for a connection at %r', (addr,))
self.make_socket()
common.socket.bind( self, addr )
self.listen(1)
if timeout:
info('timeout in %r secs', timeout)
self.timeout = Timer(timeout, lambda s=self.socket: self.handle_timeout(s))
self.timeout.start()
def handle_timeout(self, socket):
info('TIMEOUT %r', socket)
if socket is self.socket:
self.do_disconnect()
elif socket is not None:
socket.close()
def handle_expt(self):
info('handle_expt in %r', self)
self.do_disconnect()
def handle_error(self, e=None):
info('handle_error in %r', self)
import traceback
traceback.print_exc()
self.do_disconnect()
def do_disconnect(self):
'''
toss away the current connection
will try the next address immediately
'''
log.warning('do_disconnect')
self.cancel_timeout()
self.close()
if not self._accepting and self.attempts < len(self._ips):
self.try_connect()
else:
self.on_fail()
def handle_connect(self):
info('connected!')
self.cancel_timeout()
self.on_connect(self)
def handle_accept(self):
self.cancel_timeout()
conn, address = self.accept()
info('%r connection accepted (%r), canceling timeout and calling %r', self, address, self.on_connect)
self.on_connect(conn)
def cancel_timeout(self):
# Cancel any timeout.
if hasattr(self, 'timeout') and self.timeout is not None:
info('cancelling timeout')
self.timeout.cancel()
#.........这里部分代码省略.........