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


Python Queue.put_nowait方法代码示例

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


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

示例1: NLSocketPool

# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put_nowait [as 别名]
class NLSocketPool(object):
    """Pool of netlink sockets."""
    def __init__(self, size):
        if size <= 0:
            raise ValueError('Invalid socket pool size %r. Must be positive')
        self._semaphore = BoundedSemaphore(size)
        self._sockets = Queue(maxsize=size)

    @contextmanager
    def socket(self):
        """Returns a socket from the pool (creating it when needed)."""
        with self._semaphore:
            try:
                sock = self._sockets.get_nowait()
            except Empty:
                sock = _open_socket()
            try:
                yield sock
            finally:
                self._sockets.put_nowait(sock)
开发者ID:andrewlukoshko,项目名称:vdsm,代码行数:22,代码来源:__init__.py

示例2: AmqpSubscriber

# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put_nowait [as 别名]
class AmqpSubscriber(Subscriber):
    def __init__(self, amqp_chan, exchanges):
        self.channel = amqp_chan
        self.messages = Queue(maxsize=0)
        qname, _, _ = self.channel.queue_declare()
        for exchange in exchanges:
            self.channel.queue_bind(qname, exchange)
        self.channel.basic_consume(queue=qname, callback=self.callback)

    def callback(self, msg):
        self.channel.basic_ack(msg.delivery_tag)
        self.messages.put_nowait(msg.body)

    def __iter__(self):
        return self

    def next(self):
        while self.messages.empty():
            self.channel.wait()
        return self.messages.get_nowait()

    __next__ = next   # PY3
开发者ID:kuldat,项目名称:anypubsub,代码行数:24,代码来源:amqp.py

示例3: KeyboardCapture

# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put_nowait [as 别名]
class KeyboardCapture(threading.Thread):
    """Implementation of KeyboardCapture for OSX."""

    _KEYBOARD_EVENTS = set([kCGEventKeyDown, kCGEventKeyUp])

    def __init__(self):
        threading.Thread.__init__(self, name="KeyboardEventTapThread")
        self._loop = None
        self._event_queue = Queue()  # Drained by event handler thread.

        self._suppressed_keys = set()
        self.key_down = lambda key: None
        self.key_up = lambda key: None

        # Returning the event means that it is passed on
        # for further processing by others.
        #
        # Returning None means that the event is intercepted.
        #
        # Delaying too long in returning appears to cause the
        # system to ignore the tap forever after
        # (https://github.com/openstenoproject/plover/issues/484#issuecomment-214743466).
        #
        # This motivates pushing callbacks to the other side
        # of a queue of received events, so that we can return
        # from this callback as soon as possible.
        def callback(proxy, event_type, event, reference):
            SUPPRESS_EVENT = None
            PASS_EVENT_THROUGH = event

            # Don't pass on meta events meant for this event tap.
            is_unexpected_event = event_type not in self._KEYBOARD_EVENTS
            if is_unexpected_event:
                if event_type == kCGEventTapDisabledByTimeout:
                    # Re-enable the tap and hope we act faster next time
                    CGEventTapEnable(self._tap, True)
                    plover.log.warning("Keystrokes may have been missed. " + "Keyboard event tap has been re-enabled. ")
                return SUPPRESS_EVENT

            # Don't intercept the event if it has modifiers, allow
            # Fn and Numeric flags so we can suppress the arrow and
            # extended (home, end, etc...) keys.
            suppressible_modifiers = (
                kCGEventFlagMaskNumericPad | kCGEventFlagMaskSecondaryFn | kCGEventFlagMaskNonCoalesced
            )
            has_nonsupressible_modifiers = CGEventGetFlags(event) & ~suppressible_modifiers
            if has_nonsupressible_modifiers and event_type == kCGEventKeyDown:
                return PASS_EVENT_THROUGH

            keycode = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode)
            key = KEYCODE_TO_KEY.get(keycode)
            self._async_dispatch(key, event_type)
            if key in self._suppressed_keys:
                return SUPPRESS_EVENT
            return PASS_EVENT_THROUGH

        self._tap = CGEventTapCreate(
            kCGSessionEventTap,
            kCGHeadInsertEventTap,
            kCGEventTapOptionDefault,
            CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp),
            callback,
            None,
        )
        if self._tap is None:
            # Todo(hesky): See if there is a nice way to show the user what's
            # needed (or do it for them).
            raise Exception("Enable access for assistive devices.")
        CGEventTapEnable(self._tap, False)

    def run(self):
        source = CFMachPortCreateRunLoopSource(None, self._tap, 0)
        handler_thread = threading.Thread(target=self._event_handler, name="KeyEventDispatcher")
        handler_thread.start()
        self._loop = CFRunLoopGetCurrent()
        CFRunLoopAddSource(self._loop, source, kCFRunLoopCommonModes)
        CGEventTapEnable(self._tap, True)

        CFRunLoopRun()

        # Wake up event handler.
        self._event_queue.put_nowait(None)
        handler_thread.join()
        CFMachPortInvalidate(self._tap)
        CFRelease(self._tap)
        CFRunLoopSourceInvalidate(source)

    def cancel(self):
        CFRunLoopStop(self._loop)
        self.join()
        self._loop = None

    def suppress_keyboard(self, suppressed_keys=()):
        self._suppressed_keys = set(suppressed_keys)

    def _async_dispatch(self, key, event_type):
        """
        Dispatches a key string in KEYCODE_TO_KEY.values() and a CGEventType
        to the appropriate KeyboardCapture callback
        without blocking execution of its caller.
#.........这里部分代码省略.........
开发者ID:openstenoproject,项目名称:plover,代码行数:103,代码来源:osxkeyboardcontrol.py

示例4: JsonRpcServer

# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put_nowait [as 别名]
class JsonRpcServer(object):
    log = logging.getLogger("jsonrpc.JsonRpcServer")

    FILTERED_METHODS = frozenset(['Host.getAllVmStats'])

    """
    Creates new JsonrRpcServer by providing a bridge, timeout in seconds
    which defining how often we should log connections stats and thread
    factory.
    """
    def __init__(self, bridge, timeout, cif, threadFactory=None):
        self._bridge = bridge
        self._cif = cif
        self._workQueue = Queue()
        self._threadFactory = threadFactory
        self._timeout = timeout
        self._next_report = monotonic_time() + self._timeout
        self._counter = 0

    def queueRequest(self, req):
        self._workQueue.put_nowait(req)

    """
    Aggregates number of requests received by vdsm. Each request from
    a batch is added separately. After time defined by timeout we log
    number of requests.
    """
    def _attempt_log_stats(self):
        self._counter += 1
        if monotonic_time() > self._next_report:
            self.log.info('%s requests processed during %s seconds',
                          self._counter, self._timeout)
            self._next_report += self._timeout
            self._counter = 0

    def _serveRequest(self, ctx, req):
        self._attempt_log_stats()
        logLevel = logging.DEBUG
        suppress_logging = req.method in self.FILTERED_METHODS

        # VDSM should never respond to any request before all information about
        # running VMs is recovered, see https://bugzilla.redhat.com/1339291
        if not self._cif.ready:
            self.log.info("In recovery, ignoring '%s' in bridge with %s",
                          req.method, req.params)
            # TODO: take the response from the exception instead of via errCode
            ctx.requestDone(JsonRpcResponse(errCode['recovery'], None, req.id))
            return

        self.log.log(logLevel, "Calling '%s' in bridge with %s",
                     req.method, req.params)
        try:
            method = self._bridge.dispatch(req.method)
        except JsonRpcMethodNotFoundError as e:
            if req.isNotification():
                return

            ctx.requestDone(JsonRpcResponse(None, e, req.id))
            return

        try:
            params = req.params
            self._bridge.register_server_address(ctx.server_address)
            if isinstance(req.params, list):
                res = method(*params)
            else:
                res = method(**params)
            self._bridge.unregister_server_address()
        except JsonRpcError as e:
            ctx.requestDone(JsonRpcResponse(None, e, req.id))
        except Exception as e:
            self.log.exception("Internal server error")
            ctx.requestDone(JsonRpcResponse(None,
                                            JsonRpcInternalError(str(e)),
                                            req.id))
        else:
            res = True if res is None else res
            log_res = "(suppressed)" if suppress_logging else res
            self.log.log(logLevel, "Return '%s' in bridge with %s",
                         req.method, log_res)
            ctx.requestDone(JsonRpcResponse(res, None, req.id))

    @traceback(on=log.name)
    def serve_requests(self):
        while True:
            obj = self._workQueue.get()
            if obj is None:
                break

            client, server_address, msg = obj
            self._parseMessage(client, server_address, msg)

    def _parseMessage(self, client, server_address, msg):
        ctx = _JsonRpcServeRequestContext(client, server_address)

        try:
            rawRequests = json.loads(msg)
        except:
            ctx.addResponse(JsonRpcResponse(None, JsonRpcParseError(), None))
            ctx.sendReply()
#.........这里部分代码省略.........
开发者ID:andrewlukoshko,项目名称:vdsm,代码行数:103,代码来源:__init__.py

示例5: TCPHandler

# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put_nowait [as 别名]
class TCPHandler(logging.Handler):
    '''
    Python logging handler for sending JSON formatted messages over
    TCP, optionally wrapping the connection with TLSv1
    '''
    def __init__(self, host, port, ssl_ca_file=None):
        '''
        Instantiate a TCPHandler with the intent of connecting to the
        given host (string) and port (int) with or without using SSL/TLSv1
        '''
        logging.Handler.__init__(self)
        self.host = host
        self.port = port
        self.ssl_ca_file = ssl_ca_file
        self.sock = None
        self.queue = Queue(LOG_QUEUE_SIZE)
        self.connect_wait = BACKOFF_INITIAL
        self.raiseExceptions = 0

        self.hostname = socket.gethostname()
        if self.hostname.find('.') != -1:
            self.hostname = self.hostname.split('.', 1)[0]

        self.sender = threading.Thread(target=self.run)
        self.sender.setDaemon(True)
        self.sender.start()

    def connect(self):
        '''
        Create a connection with the server, sleeping for some
        period of time if connection errors have occurred recently.
        '''
        self.sock = socket.socket()
        if self.ssl_ca_file:
            self.sock = ssl.wrap_socket(self.sock,
                ssl_version=ssl.PROTOCOL_TLSv1,
                cert_reqs=ssl.CERT_REQUIRED,
                ca_certs=self.ssl_ca_file)

        INTERNAL_LOG.debug('Connecting (backoff: %.03f)' %
            self.connect_wait)
        time.sleep(self.connect_wait)
        self.sock.connect((self.host, self.port))

    def jsonify(self, record):
        '''
        Translate a LogRecord instance into a json_event
        '''
        timestamp = datetime.utcfromtimestamp(record.created)
        timestamp = timestamp.isoformat()

        fields = {
            'level': record.levelname,
            'filename': record.pathname,
            'lineno': record.lineno,
            'method': record.funcName,
        }
        if record.exc_info:
            fields['exception'] = str(record.exc_info)
            fields['traceback'] = format_exc(record.exc_info)

        log = {
            '@source_host': self.hostname,
            '@timestamp': timestamp,
            '@tags': [record.name],
            '@message': record.getMessage(),
            '@fields': fields,
        }
        return json.dumps(log)

    def emit(self, record):
        '''
        Send a LogRecord object formatted as json_event via a
        queue and worker thread.
        '''
        self.queue.put_nowait(record)

    def run(self):
        '''
        Main loop of the logger thread. All network I/O and exception handling
        originates here. Strings are consumed from self.queue and sent to
        self.sock, creating a new connection if necessary.

        If any exceptions are caught, the message is put() back on the queue
        and the exception is allowed to propagate up through
        logging.Handler.handleError(), potentially causing this thread to abort.
        '''
        INTERNAL_LOG.debug('Log I/O thread started')
        while True:
            record = self.queue.get()
            if record is None:
                break

            jsonrecord = self.jsonify(record)
            jsonrecord = '%s\n' % jsonrecord

            try:
                if self.sock is None:
                    self.connect()
                self.send(jsonrecord)
#.........这里部分代码省略.........
开发者ID:uber,项目名称:clay,代码行数:103,代码来源:logger.py


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