def countdown(left): if left == 0: print "Ignition sequence started!" with cond: cond.notify() return print "%d seconds to go" % left sys.stdout.flush() protocol.invokeLaterWithDelay(1000, countdown, left - 1)
def countdown(left): if left == 0: print("Ignition sequence started!") with cond: cond.notify() return print("%d seconds to go" % left) sys.stdout.flush() protocol.invokeLaterWithDelay(1000, countdown, left - 1)
def testTimer(): cond = threading.Condition() def countdown(left): if left == 0: print "Ignition sequence started!" with cond: cond.notify() return print "%d seconds to go" % left sys.stdout.flush() protocol.invokeLaterWithDelay(1000, countdown, left - 1) with cond: protocol.invokeLaterWithDelay(0, countdown, 10) cond.wait(15)
def testTimer(): cond = threading.Condition() def countdown(left): if left == 0: print("Ignition sequence started!") with cond: cond.notify() return print("%d seconds to go" % left) sys.stdout.flush() protocol.invokeLaterWithDelay(1000, countdown, left - 1) with cond: protocol.invokeLaterWithDelay(0, countdown, 10) cond.wait(15)
def __init__(self, target=None, *args, **kwargs): """ Construct a TCF task object and schedule it for execution. """ if target: kwargs["done"] = self.__done else: target = self.run self._target = target self._args = args self._kwargs = kwargs self._lock = threading.Condition() self.__channel = kwargs.pop("channel", None) protocol.invokeLater(self.__doRun) timeout = kwargs.pop("timeout", None) if timeout: protocol.invokeLaterWithDelay(timeout, self.cancel)
def redirect(self, peer_attrs): """ Redirect this channel to given peer using this channel remote peer locator service as a proxy. @param peer_attrs - peer that will become new remote communication endpoint of this channel """ if isinstance(peer_attrs, str): # support for redirect(peerId) map = {} map[peer.ATTR_ID] = peer_attrs peer_attrs = map channel = self assert protocol.isDispatchThread() if self.state == STATE_OPENING: self.redirect_queue.append(peer_attrs) else: assert self.state == STATE_OPEN assert self.redirect_command is None try: l = self.remote_service_by_class.get(locator.LocatorService) if not l: raise IOError("Cannot redirect channel: peer " + self.remote_peer.getID() + " has no locator service") peer_id = peer_attrs.get(peer.ATTR_ID) if peer_id and len(peer_attrs) == 1: _peer = l.getPeers().get(peer_id) if not _peer: # Peer not found, must wait for a while until peer is discovered or time out class Callback(object): found = None def __call__(self): if self.found: return channel.terminate(Exception("Peer " + peer_id + " not found")) cb = Callback() protocol.invokeLaterWithDelay(locator.DATA_RETENTION_PERIOD / 3, cb) class Listener(locator.LocatorListener): def peerAdded(self, new_peer): if new_peer.getID() == peer_id: cb.found = True channel.state = STATE_OPEN l.removeListener(self) channel.redirect_id(peer_id) l.addListener(Listener()) else: class DoneRedirect(locator.DoneRedirect): def doneRedirect(self, token, exc): assert channel.redirect_command is token channel.redirect_command = None if channel.state != STATE_OPENING: return if exc: channel.terminate(exc) channel.remote_peer = _peer channel.remote_service_by_class.clear() channel.remote_service_by_name.clear() channel.event_listeners.clear() self.redirect_command = l.redirect(peer_id, DoneRedirect()) else: class TransientPeer(peer.TransientPeer): def __init__(self, peer_attrs, parent): super(TransientPeer, self).__init__(peer_attrs) self.parent = parent def openChannel(self): c = self.parent.openChannel() c.redirect(peer_attrs) class DoneRedirect(locator.DoneRedirect): def doneRedirect(self, token, exc): assert channel.redirect_command is token channel.redirect_command = None if channel.state != STATE_OPENING: return if exc: channel.terminate(exc) parent = channel.remote_peer channel.remote_peer = TransientPeer(peer_attrs, parent) channel.remote_service_by_class.clear() channel.remote_service_by_name.clear() channel.event_listeners.clear() self.redirect_command = l.redirect(peer_attrs, DoneRedirect()) self.state = STATE_OPENING except Exception as x: self.terminate(x)