Ejemplo n.º 1
0
 def add_done_callback(self, fn):
     """ add_done_callback(fn)
     
     Attaches the callable fn to the future. fn will be called, with
     the future as its only argument, when the future is cancelled or
     finishes running.
     
     Added callables are called in the order that they were added. If
     the callable raises a Exception subclass, it will be logged and
     ignored. If the callable raises a BaseException subclass, the
     behavior is undefined.
     
     If the future has already completed or been cancelled, fn will be
     called immediately.
     
     """
     
     # Check
     if not hasattr(fn, '__call__'):
         raise ValueError('add_done_callback expects a callable.')
     
     # Add
     if self.done():
         yoton.call_later(fn, 0, self)
     else:
         self._callbacks.append(fn)
Ejemplo n.º 2
0
    def add_done_callback(self, fn):
        """ add_done_callback(fn)
        
        Attaches the callable fn to the future. fn will be called, with 
        the future as its only argument, when the future is cancelled or 
        finishes running.
        
        Added callables are called in the order that they were added. If 
        the callable raises a Exception subclass, it will be logged and 
        ignored. If the callable raises a BaseException subclass, the 
        behavior is undefined.
        
        If the future has already completed or been cancelled, fn will be 
        called immediately.
        
        """

        # Check
        if not hasattr(fn, '__call__'):
            raise ValueError('add_done_callback expects a callable.')

        # Add
        if self.done():
            yoton.call_later(fn, 0, self)
        else:
            self._callbacks.append(fn)
Ejemplo n.º 3
0
    def set_result(self, result):
        """ set_result(result)
        
        Sets the result of the work associated with the Future to result.
        This method should only be used by Executor implementations and
        unit tests.
        
        """

        # Set result if indeed in running state
        if self._status == 1:
            self._result = result
            self._status = 4
            for fn in self._callbacks:
                yoton.call_later(fn, 0, self)
Ejemplo n.º 4
0
 def set_result(self, result):
     """ set_result(result)
     
     Sets the result of the work associated with the Future to result.
     This method should only be used by Executor implementations and
     unit tests.
     
     """
     
     # Set result if indeed in running state
     if self._status == 1:
         self._result = result
         self._status = 4
         for fn in self._callbacks:
             yoton.call_later(fn, 0, self)
Ejemplo n.º 5
0
    def set_exception(self, exception):
        """ set_exception(exception)
        
        Sets the result of the work associated with the Future to the 
        Exception exception. This method should only be used by Executor 
        implementations and unit tests.
        
        """

        # Check
        if isinstance(exception, basestring):
            exception = Exception(exception)
        if not isinstance(exception, Exception):
            raise ValueError('exception must be an Exception instance.')

        # Set result if indeed in running state
        if self._status == 1:
            self._result = exception
            self._status = 3
            for fn in self._callbacks:
                yoton.call_later(fn, 0, self)
Ejemplo n.º 6
0
 def set_exception(self, exception):
     """ set_exception(exception)
     
     Sets the result of the work associated with the Future to the
     Exception exception. This method should only be used by Executor
     implementations and unit tests.
     
     """
     
     # Check
     if isinstance(exception, basestring):
         exception = Exception(exception)
     if not isinstance(exception, Exception):
         raise ValueError('exception must be an Exception instance.')
     
     # Set result if indeed in running state
     if self._status == 1:
         self._result = exception
         self._status = 3
         for fn in self._callbacks:
             yoton.call_later(fn, 0, self)
Ejemplo n.º 7
0
    def cancel(self):
        """ cancel()
        
        Attempt to cancel the call. If the call is currently being executed
        and cannot be cancelled then the method will return False, otherwise
        the call will be cancelled and the method will return True.
        
        """

        if self._status == 1:
            # Running, cannot cancel
            return False
        elif self._status == 0:
            # Cancel now
            self._status = 2
            self._send('req-')
            for fn in self._callbacks:
                yoton.call_later(fn, 0, self)
            return True
        else:
            # Already done or canceled
            return True
Ejemplo n.º 8
0
 def cancel(self):
     """ cancel()
     
     Attempt to cancel the call. If the call is currently being executed
     and cannot be cancelled then the method will return False, otherwise
     the call will be cancelled and the method will return True.
     
     """
     
     if self._status == 1:
         # Running, cannot cancel
         return False
     elif self._status == 0:
         # Cancel now
         self._status = 2
         self._send('req-')
         for fn in self._callbacks:
             yoton.call_later(fn, 0, self)
         return True
     else:
         # Already done or canceled
         return True
Ejemplo n.º 9
0
# Get status (but wait first)
time.sleep(0.3)
print(sub2.recv())

# Ask status again (simply gives last message)
print(sub2.recv())


## Using received signals


def on_new_state(channel):
    state = channel.recv()
    print("%i received state %s" % (id(channel), state))
    if state == "stop":
        yoton.stop_event_loop()


# Bind
sub1.received.bind(on_new_state)
sub2.received.bind(on_new_state)

# Have some calls made
yoton.call_later(pub1.send, 1.0, "hello")
yoton.call_later(pub1.send, 1.5, "there")
yoton.call_later(pub1.send, 2.0, "now")
yoton.call_later(pub1.send, 2.5, "stop")

# Go!
yoton.start_event_loop()
Ejemplo n.º 10
0
# Create a context and a sub channel
ct2 = yoton.Context(verbose=verbosity)
sub = yoton.SubChannel(ct2, 'foo')

# Connect, set channel to event driven mode
ct2.connect('publichost:test')

# Create message handler
def message_handler():
    message = sub.recv(False)
    if message:
        print(message)
        if message.lower() == 'stop':
            yoton.stop_event_loop()

# Bind handler to a timer
timer = yoton.Timer(0.1, False)
timer.bind(message_handler)
timer.start()


# Send messages
yoton.call_later(pub.send, 8, 'stop')
yoton.call_later(pub.send, 2, '2 seconds')
yoton.call_later(pub.send, 4, '4 seconds')
yoton.call_later(pub.send, 6, 'almost done')

# Enter event loop
yoton.start_event_loop()

Ejemplo n.º 11
0
# Create a context and a sub channel
ct2 = yoton.Context(verbose=verbosity)
sub = yoton.SubChannel(ct2, 'foo')

# Connect, set channel to event driven mode
ct2.connect('publichost:test')

# Create message handler 
def message_handler():
    message = sub.recv(False)
    if message:
        print(message)
        if message.lower() == 'stop':
            yoton.stop_event_loop()

# Bind handler to a timer
timer = yoton.Timer(0.1, False)
timer.bind(message_handler)
timer.start()


# Send messages
yoton.call_later(pub.send, 8, 'stop')
yoton.call_later(pub.send, 2, '2 seconds')
yoton.call_later(pub.send, 4, '4 seconds')
yoton.call_later(pub.send, 6, 'almost done')

# Enter event loop
yoton.start_event_loop()

Ejemplo n.º 12
0
# Create a context and a sub channel
ct2 = yoton.Context(verbose=verbosity)
sub = yoton.SubChannel(ct2, "foo")

# Connect, set channel to event driven mode
ct2.connect("publichost:test")

# Create message handler
def message_handler():
    message = sub.recv(False)
    if message:
        print(message)
        if message.lower() == "stop":
            yoton.stop_event_loop()


# Bind handler to a timer
timer = yoton.Timer(0.1, False)
timer.bind(message_handler)
timer.start()


# Send messages
yoton.call_later(pub.send, 8, "stop")
yoton.call_later(pub.send, 2, "2 seconds")
yoton.call_later(pub.send, 4, "4 seconds")
yoton.call_later(pub.send, 6, "almost done")

# Enter event loop
yoton.start_event_loop()
Ejemplo n.º 13
0
ct4.connect('localhost:test3')

# Get status (but wait first)
time.sleep(0.3)
print(sub2.recv())

# Ask status again (simply gives last message)
print(sub2.recv())


## Using received signals

def on_new_state(channel):
    state = channel.recv()
    print('%i received state %s' % (id(channel), state))
    if state == 'stop':
        yoton.stop_event_loop()

# Bind
sub1.received.bind(on_new_state)
sub2.received.bind(on_new_state)

# Have some calls made
yoton.call_later(pub1.send, 1.0, 'hello')
yoton.call_later(pub1.send, 1.5, 'there')
yoton.call_later(pub1.send, 2.0, 'now')
yoton.call_later(pub1.send, 2.5, 'stop')

# Go!
yoton.start_event_loop()