当前位置: 首页>>代码示例>>Python>>正文


Python ZMQStream.flush方法代码示例

本文整理汇总了Python中zmq.eventloop.zmqstream.ZMQStream.flush方法的典型用法代码示例。如果您正苦于以下问题:Python ZMQStream.flush方法的具体用法?Python ZMQStream.flush怎么用?Python ZMQStream.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zmq.eventloop.zmqstream.ZMQStream的用法示例。


在下文中一共展示了ZMQStream.flush方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SubQueue

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class SubQueue(BaseQueue):

    def __init__(self, connect, callback, ioloop=None):
        super().__init__(zmq.PULL)
        self.socket.connect(connect)
        self.stream = ZMQStream(self.socket, ioloop)
        self.stream.on_recv(callback)
        self.stream.flush()
开发者ID:Teleinformatyka,项目名称:RedSparrow,代码行数:10,代码来源:queue.py

示例2: RequestQueue

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class RequestQueue(BaseQueue):

    def __init__(self, connect, ioloop=None):
        super().__init__(zmq.REQ)
        self.socket.connect(connect)
        self.socket.SNDTIMEO = 2000
        self.socket.RCVTIMEO = 2000
        self.stream = ZMQStream(self.socket, ioloop)
        self.stream.flush()

    def __getattr__(self, name):
        return getattr(self.stream, name)
开发者ID:Teleinformatyka,项目名称:RedSparrow,代码行数:14,代码来源:queue.py

示例3: ReplyQueue

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class ReplyQueue(BaseQueue):

    def __init__(self, bind, callback, ioloop=None):
        super().__init__(zmq.REP)
        self.socket.bind(bind)
        self.socket.SNDTIMEO = 1000
        self.socket.RCVTIMEO = 1000
        self.stream = ZMQStream(self.socket, ioloop)
        self.stream.on_recv(callback)
        self.stream.flush()

    def __getattr__(self, name):
        return getattr(self.stream, name)
开发者ID:Teleinformatyka,项目名称:RedSparrow,代码行数:15,代码来源:queue.py

示例4: PubQueue

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class PubQueue(BaseQueue):

    def __init__(self, bind, ioloop=None):
        super().__init__(zmq.PUSH)
        self.socket.bind(bind)
        self.stream = ZMQStream(self.socket, ioloop)
        self.stream.on_send(self.__on_send)

    def __on_send(self, msq, status):
        print(msq, status)

    def send(self, data):
        self.stream.send(data)
        self.stream.flush()

    def send_string(self, data):
        self.stream.send_string(data)
        self.stream.flush()
开发者ID:Teleinformatyka,项目名称:RedSparrow,代码行数:20,代码来源:queue.py

示例5: ZmqWorker

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class ZmqWorker(object, LoggingMixin):
    """
    This is the ZMQ worker implementation.

    The worker will register a :class:`ZMQStream` with the configured
    :class:`zmq.Socket` and :class:`zmq.eventloop.ioloop.IOLoop` instance.

    Upon `ZMQStream.on_recv` the configured `processors` will be executed
    with the deserialized context and the result will be published through the
    configured `zmq.socket`.
    """

    def __init__(self, insocket, outsocket, mgmt, processing, log_handler,
            log_level, io_loop=None):
        """
        Initialize the `ZMQStream` with the `insocket` and `io_loop` and store
        the `outsocket`.

        `insocket` should be of the type `zmq.socket.PULL` `outsocket` should
        be of the type `zmq.socket.PUB`

        `mgmt` is an instance of `spyder.core.mgmt.ZmqMgmt` that handles
        communication between master and worker processes.
        """
        LoggingMixin.__init__(self, log_handler, log_level)

        self._insocket = insocket
        self._io_loop = io_loop or IOLoop.instance()
        self._outsocket = outsocket

        self._processing = processing
        self._mgmt = mgmt
        self._in_stream = ZMQStream(self._insocket, self._io_loop)
        self._out_stream = ZMQStream(self._outsocket, self._io_loop)

    def _quit(self, msg):
        """
        The worker is quitting, stop receiving messages.
        """
        if ZMQ_SPYDER_MGMT_WORKER_QUIT == msg.data:
            self.stop()

    def _receive(self, msg):
        """
        We have a message!

        `msg` is a serialized version of a `DataMessage`.
        """
        message = DataMessage(msg)

        try:
            # this is the real work we want to do
            curi = self._processing(message.curi)
            message.curi = curi
        except:
            # catch any uncaught exception and only log it as CRITICAL
            self._logger.critical(
                    "worker::Uncaught exception executing the worker for URL %s!" %
                    (message.curi.url,))
            self._logger.critical("worker::%s" % (traceback.format_exc(),))

        # finished, now send the result back to the master
        self._out_stream.send_multipart(message.serialize())

    def start(self):
        """
        Start the worker.
        """
        self._mgmt.add_callback(ZMQ_SPYDER_MGMT_WORKER, self._quit)
        self._in_stream.on_recv(self._receive)

    def stop(self):
        """
        Stop the worker.
        """
        # stop receiving
        self._in_stream.stop_on_recv()
        self._mgmt.remove_callback(ZMQ_SPYDER_MGMT_WORKER, self._quit)
        # but work on anything we might already have
        self._in_stream.flush()
        self._out_stream.flush()

    def close(self):
        """
        Close all open sockets.
        """
        self._in_stream.close()
        self._insocket.close()
        self._out_stream.close()
        self._outsocket.close()
开发者ID:Big-Data,项目名称:Spyder,代码行数:92,代码来源:worker.py

示例6: __init__

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class Client:

  def __init__(self):
    self._current_packet_id = 0
    self._request_response_map = {}
    self._new_response_condition = Condition()
    self._packet_id_lock = Lock()
    self._notification_callbacks = []

  def connect_to_server(self, server):
    self.context = zmq.Context()
    self.socket = self.context.socket(zmq.DEALER)

    tmpdir = tempfile.gettempdir()
    connection_string  = 'ipc://%s/%s_%s' %  (tmpdir, 'zmq', server)
    self.socket.connect(connection_string)

    io_loop = ioloop.IOLoop(ioloop.ZMQPoller())

    self.stream = ZMQStream(self.socket, io_loop)

    # create partial function that has self as first argument
    callback = partial(_on_recv, self)
    self.stream.on_recv(callback)
    self.event_loop = EventLoop(io_loop)
    self.event_loop.start()

  def disconnect(self):
    self.stream.flush()
    self.event_loop.stop()
    self.socket.close()

  def register_notification_callback(self, callback):
    # check a valid function has been past
    assert callable(callback)
    self._notification_callbacks.append(callback)

  def request_queue_list_update(self):
    pass

  def submit_job_request(self, request, timeout=None):
    params = JsonRpc.jobrequest_to_json_params(request)
    packet_id = self._next_packet_id()
    jsonrpc = JsonRpc.generate_request(packet_id,
                                      'submitJob',
                                      params)

    self._send_request(packet_id, jsonrpc)
    response = self._wait_for_response(packet_id, timeout)

    # Timeout
    if response == None:
      return None

    # if we an error occurred then throw an exception
    if 'error' in response:
      exception = JobRequestException(response['id'],
                                      response['error']['code'],
                                      response['error']['message'])
      raise exception

    # otherwise return the molequeue id
    return response['result']['moleQueueId']

  def cancel_job(self):
    # TODO
    pass

  def lookup_job(self, molequeue_id, timeout=None):

    params = {'moleQueueId': molequeue_id}

    packet_id = self._next_packet_id()
    jsonrpc = JsonRpc.generate_request(packet_id,
                                      'lookupJob',
                                      params)

    self._send_request(packet_id, jsonrpc)
    response = self._wait_for_response(packet_id, timeout)

    # Timeout
    if response == None:
      return None

    # if we an error occurred then throw an exception
    if 'error' in response:
      exception = JobRequestInformationException(response['id'],
                                                 reponse['error']['data'],
                                                 reponse['error']['code'],
                                                 reponse['error']['message'])
      raise exception

    jobrequest = JsonRpc.json_to_jobrequest(response)

    return jobrequest


  def _on_response(self, packet_id, msg):
    if packet_id in self._request_response_map:
      self._new_response_condition.acquire()
#.........这里部分代码省略.........
开发者ID:cjh1,项目名称:molequeue,代码行数:103,代码来源:client.py

示例7: __init__

# 需要导入模块: from zmq.eventloop.zmqstream import ZMQStream [as 别名]
# 或者: from zmq.eventloop.zmqstream.ZMQStream import flush [as 别名]
class Stream:
    '''
    This is the main class that interacts with the zmq library to send, and
    receive messages.
    '''
    def __init__(self, socket, stream_info, path, on_recv=None, on_send=None, loop=None):
        '''
        Initializes instance of Stream.

        @param socket - zmq socket that has alredy been bound
        @param stream_info - this streams definition from the yaml config
        @param path - path to the socket on the disk
        @parma on_recv - callback that processes received messages
        @parma on_send - callback that processes sent messages
        @param loop - loop this socket will belong to. Default is global async loop.
        '''
        self._path = path
        self._recv_type = stream_info.recv_type
        self._send_type = stream_info.send_type
        self._reply_type = stream_info.reply_type
        self._on_recv = on_recv
        self._on_send = on_send

        self._stream = ZMQStream(socket, io_loop=loop)

        if self._on_recv is not None:
            self._stream.on_recv(self._recv_wrapper)
        if self._on_send is not None:
            self._stream.on_send(self._send_wrapper)

    @property
    def recv_type(self):
        return self._recv_type

    @property
    def send_type(self):
        return self._send_type

    @property
    def reply_type(self):
        return self._reply_type

    def on_send(self, callback):
        '''
        Set the callback to be invoked on every send command. on_send(None)
        disables this callback.

        @param callback - 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.
        '''
        self._on_send = callback
        if callback is None:
            self._stream.on_send(None)
        else:
            self._stream.on_send(self._send_wrapper)

    def on_recv(self, callback):
        '''
        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.

        @param callback - 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.
        '''
        self._on_recv = callback
        if callback is None:
            self._stream.on_recv(None)
        else:
            self._stream.on_recv(self._recv_wrapper)

    def flush(self, flag=3, limit=None):
        '''
        Flush pending messages.

        This method safely handles all pending incoming and/or outgoing
        messages, bypassing the inner loop, passing them to the registered
        callbacks.

        A limit can be specified, to prevent blocking under high load.

        flush will return the first time ANY of these conditions are met:
        No more events matching the flag are pending.
        the total number of events handled reaches the limit.

        @param flag - 0MQ poll flags. If flag|POLLIN, recv events will be
                      flushed. If flag|POLLOUT, send events will be flushed.
                      Both flags can be set at once, which is the default.
        @param limit - None, or int. Optional. The maximum number of messages
                       to send or receive. Both send and receive count against
                       this limit
        @returns int - count of events handled
        '''
        return self._stream.flush(flag, limit)

    def send(self, msg, **kwds):
#.........这里部分代码省略.........
开发者ID:ayeganov,项目名称:Mobius,代码行数:103,代码来源:stream.py


注:本文中的zmq.eventloop.zmqstream.ZMQStream.flush方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。