Esempio n. 1
0
 def on_send(self, callback):
     """Register a callback to be called on each send
     There will be two arguments: the message being sent (always a list), 
     and the return result of socket.send_multipart(msg).
     
     Non-copying sends return a MessageTracker object whose
     `done` attribute will be True when the send is complete. 
     This allows users to track when an object is safe to write to
     again.
     
     The second argument will always be None if copy=True
     on the send.
     
     on_send(None) disables recv event polling.
     
     Parameters
     ----------
     
     callback : callable
         callback must take exactly two arguments, which will be
         There will be two arguments: the message being sent (always a list), 
         and the return result of socket.send_multipart(msg) - 
         MessageTracker or None.
         
         if callback is None, send callbacks are disabled.
     """
     self._check_closed()
     assert callback is None or callable(callback)
     self._send_callback = stack_context.wrap(callback)
Esempio n. 2
0
 def on_recv(self, callback, copy=True):
     """Register a callback to be called when a message is ready to recv.
     There can be only one callback registered at a time, so each
     call to on_recv replaces previously registered callbacks.
     
     on_recv(None) disables recv event polling.
     
     Parameters
     ----------
     
     callback : callable
         callback must take exactly one argument, which will be a
         list, as returned by socket.recv_multipart()
         if callback is None, recv callbacks are disabled.
     copy : bool
         copy is passed directly to recv, so if copy is False,
         callback will receive Message objects. If copy is True,
         then callback will receive bytes/str objects.
     
     Returns : None
     """
     
     assert callback is None or callable(callback)
     self._recv_callback = stack_context.wrap(callback)
     self._recv_copy = copy
     if callback is None:
         self._drop_io_state(zmq.POLLIN)
     else:
         self._add_io_state(zmq.POLLIN)
Esempio n. 3
0
 def on_send(self, callback):
     """Register a callback to be called on each send
     There will be two arguments: the message being sent (always a list), 
     and the return result of socket.send_multipart(msg).
     
     Non-copying sends return a MessageTracker object whose
     `done` attribute will be True when the send is complete. 
     This allows users to track when an object is safe to write to
     again.
     
     The second argument will always be None if copy=True
     on the send.
     
     on_send(None) disables recv event polling.
     
     Parameters
     ----------
     
     callback : callable
         callback must take exactly two arguments, which will be
         There will be two arguments: the message being sent (always a list), 
         and the return result of socket.send_multipart(msg) - 
         MessageTracker or None.
         
         if callback is None, send callbacks are disabled.
     """
     assert callback is None or callable(callback)
     self._send_callback = stack_context.wrap(callback)
Esempio n. 4
0
    def on_recv(self, callback, copy=True):
        """Register a callback for when a message is ready to recv.
        
        There can be only one callback registered at a time, so each
        call to `on_recv` replaces previously registered callbacks.
        
        on_recv(None) disables recv event polling.
        
        Use on_recv_stream(callback) instead, to register a callback that will receive
        both this ZMQStream and the message, instead of just the message.
        
        Parameters
        ----------
        
        callback : callable
            callback must take exactly one argument, which will be a
            list, as returned by socket.recv_multipart()
            if callback is None, recv callbacks are disabled.
        copy : bool
            copy is passed directly to recv, so if copy is False,
            callback will receive Message objects. If copy is True,
            then callback will receive bytes/str objects.
        
        Returns : None
        """

        self._check_closed()
        assert callback is None or callable(callback)
        self._recv_callback = stack_context.wrap(callback)
        self._recv_copy = copy
        if callback is None:
            self._drop_io_state(self.io_loop.READ)
        else:
            self._add_io_state(self.io_loop.READ)
Esempio n. 5
0
 def on_recv(self, callback, copy=True):
     """Register a callback for when a message is ready to recv.
     
     There can be only one callback registered at a time, so each
     call to `on_recv` replaces previously registered callbacks.
     
     on_recv(None) disables recv event polling.
     
     Use on_recv_stream(callback) instead, to register a callback that will receive
     both this ZMQStream and the message, instead of just the message.
     
     Parameters
     ----------
     
     callback : callable
         callback must take exactly one argument, which will be a
         list, as returned by socket.recv_multipart()
         if callback is None, recv callbacks are disabled.
     copy : bool
         copy is passed directly to recv, so if copy is False,
         callback will receive Message objects. If copy is True,
         then callback will receive bytes/str objects.
     
     Returns : None
     """
     
     self._check_closed()
     assert callback is None or callable(callback)
     self._recv_callback = stack_context.wrap(callback)
     self._recv_copy = copy
     if callback is None:
         self._drop_io_state(self.io_loop.READ)
     else:
         self._add_io_state(self.io_loop.READ)
Esempio n. 6
0
    def on_recv(self, callback, copy=True):
        """Register a callback to be called when a message is ready to recv.
        There can be only one callback registered at a time, so each
        call to on_recv replaces previously registered callbacks.
        
        on_recv(None) disables recv event polling.
        
        Parameters
        ----------
        
        callback : callable
            callback must take exactly one argument, which will be a
            list, as returned by socket.recv_multipart()
            if callback is None, recv callbacks are disabled.
        copy : bool
            copy is passed directly to recv, so if copy is False,
            callback will receive Message objects. If copy is True,
            then callback will receive bytes/str objects.
        
        Returns : None
        """

        assert callback is None or callable(callback)
        self._recv_callback = stack_context.wrap(callback)
        self._recv_copy = copy
        if callback is None:
            self._drop_io_state(zmq.POLLIN)
        else:
            self._add_io_state(zmq.POLLIN)
Esempio n. 7
0
    def add_callback(self, callback):
        """Calls the given callback on the next I/O loop iteration.

        This is thread safe because set.add is an atomic operation. The rest
        of the API is not thread safe.
        """
        self._callbacks.add(stack_context.wrap(callback))
        self._wake()
Esempio n. 8
0
    def add_callback(self, callback):
        """Calls the given callback on the next I/O loop iteration.

        This is thread safe because set.add is an atomic operation. The rest
        of the API is not thread safe.
        """
        self._callbacks.add(stack_context.wrap(callback))
        self._wake()
Esempio n. 9
0
 def on_err(self, callback):
     """register a callback to be called on POLLERR events
     with no arguments.
     
     Parameters
     ----------
     
     callback : callable
         callback will be passed no arguments.
     """
     self._errback = stack_context.wrap(callback)
Esempio n. 10
0
 def on_err(self, callback):
     """register a callback to be called on POLLERR events
     with no arguments.
     
     Parameters
     ----------
     
     callback : callable
         callback will be passed no arguments.
     """
     assert callback is None or callable(callback)
     self._errback = stack_context.wrap(callback)
Esempio n. 11
0
    def add_timeout(self, deadline, callback):
        """Calls the given callback at the time deadline from the I/O loop.

        Returns a handle that may be passed to remove_timeout to cancel.

        ``deadline`` may be a number denoting a unix timestamp (as returned
        by ``time.time()`` or a ``datetime.timedelta`` object for a deadline
        relative to the current time.
        """
        timeout = _Timeout(deadline, stack_context.wrap(callback))
        heapq.heappush(self._timeouts, timeout)
        return timeout
Esempio n. 12
0
    def __init__(self, m2req, stream, request_callback, no_keep_alive=False,
            xheaders=False):
        self.m2req = m2req
        self.stream = stream
        self.request_callback = request_callback
        self.no_keep_alive = no_keep_alive
        self.xheaders = xheaders

        self._request = None
        self._request_finished = False

        self._execute = stack_context.wrap(self._begin_request)
        self._execute()
Esempio n. 13
0
    def add_timeout(self, deadline, callback):
        """Calls the given callback at the time deadline from the I/O loop.

        Returns a handle that may be passed to remove_timeout to cancel.

        ``deadline`` may be a number denoting a unix timestamp (as returned
        by ``time.time()`` or a ``datetime.timedelta`` object for a deadline
        relative to the current time.

        Note that it is not safe to call `add_timeout` from other threads.
        Instead, you must use `add_callback` to transfer control to the
        IOLoop's thread, and then call `add_timeout` from there.
        """
        timeout = _Timeout(deadline, stack_context.wrap(callback))
        heapq.heappush(self._timeouts, timeout)
        return timeout
Esempio n. 14
0
    def add_timeout(self, deadline, callback):
        """Calls the given callback at the time deadline from the I/O loop.

        Returns a handle that may be passed to remove_timeout to cancel.

        ``deadline`` may be a number denoting a unix timestamp (as returned
        by ``time.time()`` or a ``datetime.timedelta`` object for a deadline
        relative to the current time.

        Note that it is not safe to call `add_timeout` from other threads.
        Instead, you must use `add_callback` to transfer control to the
        IOLoop's thread, and then call `add_timeout` from there.
        """
        timeout = _Timeout(deadline, stack_context.wrap(callback))
        heapq.heappush(self._timeouts, timeout)
        return timeout
Esempio n. 15
0
    def __init__(self,
                 m2req,
                 stream,
                 request_callback,
                 no_keep_alive=False,
                 xheaders=False):
        self.m2req = m2req
        self.stream = stream
        self.request_callback = request_callback
        self.no_keep_alive = no_keep_alive
        self.xheaders = xheaders

        self._request = None
        self._request_finished = False

        self._execute = stack_context.wrap(self._begin_request)
        self._execute()
Esempio n. 16
0
    def add_callback(self, callback):
        """Calls the given callback on the next I/O loop iteration.

        It is safe to call this method from any thread at any time.
        Note that this is the *only* method in IOLoop that makes this
        guarantee; all other interaction with the IOLoop must be done
        from that IOLoop's thread.  add_callback() may be used to transfer
        control from other threads to the IOLoop's thread.
        """
        with self._callback_lock:
            list_empty = not self._callbacks
            self._callbacks.append(stack_context.wrap(callback))
        if list_empty and thread_get_ident() != self._thread_ident:
            # If we're in the IOLoop's thread, we know it's not currently
            # polling.  If we're not, and we added the first callback to an
            # empty list, we may need to wake it up (it may wake up on its
            # own, but an occasional extra wake is harmless).  Waking
            # up a polling IOLoop is relatively expensive, so we try to
            # avoid it when we can.
            self._waker.wake()
Esempio n. 17
0
    def add_callback(self, callback):
        """Calls the given callback on the next I/O loop iteration.

        It is safe to call this method from any thread at any time.
        Note that this is the *only* method in IOLoop that makes this
        guarantee; all other interaction with the IOLoop must be done
        from that IOLoop's thread.  add_callback() may be used to transfer
        control from other threads to the IOLoop's thread.
        """
        with self._callback_lock:
            list_empty = not self._callbacks
            self._callbacks.append(stack_context.wrap(callback))
        if list_empty and thread_get_ident() != self._thread_ident:
            # If we're in the IOLoop's thread, we know it's not currently
            # polling.  If we're not, and we added the first callback to an
            # empty list, we may need to wake it up (it may wake up on its
            # own, but an occasional extra wake is harmless).  Waking
            # up a polling IOLoop is relatively expensive, so we try to
            # avoid it when we can.
            self._waker.wake()
Esempio n. 18
0
 def on_send(self, callback):
     """Register a callback to be called on each send
     
     There will be two arguments::
     
         callback(msg, status)
     
     * `msg` will be the list of sendable objects that was just sent
     * `status` will be the return result of socket.send_multipart(msg) -
       MessageTracker or None.
     
     Non-copying sends return a MessageTracker object whose
     `done` attribute will be True when the send is complete.
     This allows users to track when an object is safe to write to
     again.
     
     The second argument will always be None if copy=True
     on the send.
     
     Use on_send_stream(callback) to register a callback that will be passed
     this ZMQStream as the first argument, in addition to the other two.
     
     on_send(None) disables recv event polling.
     
     Parameters
     ----------
     
     callback : callable
         callback must take exactly two arguments, which will be
         the message being sent (always a list),
         and the return result of socket.send_multipart(msg) -
         MessageTracker or None.
         
         if callback is None, send callbacks are disabled.
     """
     
     self._check_closed()
     assert callback is None or callable(callback)
     self._send_callback = stack_context.wrap(callback)
Esempio n. 19
0
 def set_close_callback(self, callback):
     """Call the given callback when the stream is closed."""
     self._close_callback = stack_context.wrap(callback)
Esempio n. 20
0
 def set_close_callback(self, callback):
     """Call the given callback when the stream is closed."""
     self._close_callback = stack_context.wrap(callback)
Esempio n. 21
0
 def add_handler(self, fd, handler, events):
     """Registers the given handler to receive the given events for fd."""
     self._handlers[fd] = stack_context.wrap(handler)
     self._impl.register(fd, events | self.ERROR)
Esempio n. 22
0
 def add_handler(self, fd, handler, events):
     """Registers the given handler to receive the given events for fd."""
     self._handlers[fd] = stack_context.wrap(handler)
     self._impl.register(fd, events | self.ERROR)
Esempio n. 23
0
 def add_timeout(self, deadline, callback):
     """Calls the given callback at the time deadline from the I/O loop."""
     timeout = _Timeout(deadline, stack_context.wrap(callback))
     bisect.insort(self._timeouts, timeout)
     return timeout
Esempio n. 24
0
 def add_timeout(self, deadline, callback):
     """Calls the given callback at the time deadline from the I/O loop."""
     timeout = _Timeout(deadline, stack_context.wrap(callback))
     bisect.insort(self._timeouts, timeout)
     return timeout