當前位置: 首頁>>代碼示例>>Python>>正文


Python selectors.EVENT_WRITE屬性代碼示例

本文整理匯總了Python中selectors.EVENT_WRITE屬性的典型用法代碼示例。如果您正苦於以下問題:Python selectors.EVENT_WRITE屬性的具體用法?Python selectors.EVENT_WRITE怎麽用?Python selectors.EVENT_WRITE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在selectors的用法示例。


在下文中一共展示了selectors.EVENT_WRITE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: register

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def register(self, fileobj, events, data=None):
        if (not events) or (events & ~(EVENT_READ | EVENT_WRITE)):
            raise ValueError("Invalid events: {!r}".format(events))

        key = selectors.SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)

        if key.fd in self._fd_to_key:
            raise KeyError("{!r} (FD {}) is already registered".format(fileobj, key.fd))

        self._fd_to_key[key.fd] = key

        if events & EVENT_READ:
            notifier = QtCore.QSocketNotifier(key.fd, QtCore.QSocketNotifier.Read)
            notifier.activated.connect(self.__on_read_activated)
            self.__read_notifiers[key.fd] = notifier
        if events & EVENT_WRITE:
            notifier = QtCore.QSocketNotifier(key.fd, QtCore.QSocketNotifier.Write)
            notifier.activated.connect(self.__on_write_activated)
            self.__write_notifiers[key.fd] = notifier

        return key 
開發者ID:gmarull,項目名稱:asyncqt,代碼行數:23,代碼來源:_unix.py

示例2: _process_event

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def _process_event(self, key, mask):
        """Selector has delivered us an event."""
        self._logger.debug('Processing event with key {} and mask {}'.format(key, mask))
        fileobj, (reader, writer) = key.fileobj, key.data
        if mask & selectors.EVENT_READ and reader is not None:
            if reader._cancelled:
                self.remove_reader(fileobj)
            else:
                self._logger.debug('Invoking reader callback: {}'.format(reader))
                reader._run()
        if mask & selectors.EVENT_WRITE and writer is not None:
            if writer._cancelled:
                self.remove_writer(fileobj)
            else:
                self._logger.debug('Invoking writer callback: {}'.format(writer))
                writer._run() 
開發者ID:gmarull,項目名稱:asyncqt,代碼行數:18,代碼來源:_unix.py

示例3: test_unregister_after_fd_close_and_reuse

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def test_unregister_after_fd_close_and_reuse(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)
        rd, wr = self.make_socketpair()
        r, w = rd.fileno(), wr.fileno()
        s.register(r, selectors.EVENT_READ)
        s.register(w, selectors.EVENT_WRITE)
        rd2, wr2 = self.make_socketpair()
        rd.close()
        wr.close()
        os.dup2(rd2.fileno(), r)
        os.dup2(wr2.fileno(), w)
        self.addCleanup(os.close, r)
        self.addCleanup(os.close, w)
        s.unregister(r)
        s.unregister(w) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_selectors.py

示例4: test_timeout

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def test_timeout(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)

        rd, wr = self.make_socketpair()

        s.register(wr, selectors.EVENT_WRITE)
        t = time()
        self.assertEqual(1, len(s.select(0)))
        self.assertEqual(1, len(s.select(-1)))
        self.assertLess(time() - t, 0.5)

        s.unregister(wr)
        s.register(rd, selectors.EVENT_READ)
        t = time()
        self.assertFalse(s.select(0))
        self.assertFalse(s.select(-1))
        self.assertLess(time() - t, 0.5)

        t0 = time()
        self.assertFalse(s.select(1))
        t1 = time()
        dt = t1 - t0
        # Tolerate 2.0 seconds for very slow buildbots
        self.assertTrue(0.8 <= dt <= 2.0, dt) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:27,代碼來源:test_selectors.py

示例5: __repr__

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def __repr__(self):
        info = [self.__class__.__name__]
        if self._pipe is None:
            info.append('closed')
        elif self._closing:
            info.append('closing')
        info.append(f'fd={self._fileno}')
        selector = getattr(self._loop, '_selector', None)
        if self._pipe is not None and selector is not None:
            polling = selector_events._test_selector_event(
                selector, self._fileno, selectors.EVENT_WRITE)
            if polling:
                info.append('polling')
            else:
                info.append('idle')

            bufsize = self.get_write_buffer_size()
            info.append(f'bufsize={bufsize}')
        elif self._pipe is not None:
            info.append('open')
        else:
            info.append('closed')
        return '<{}>'.format(' '.join(info)) 
開發者ID:CedricGuillemet,項目名稱:Imogen,代碼行數:25,代碼來源:unix_events.py

示例6: _remove_writer

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def _remove_writer(self, fd):
        """Remove a writer callback."""
        if self.is_closed():
            return False
        try:
            key = self._selector.get_key(fd)
        except KeyError:
            return False
        else:
            mask, (reader, writer) = key.events, key.data
            # Remove both writer and connector.
            mask &= ~selectors.EVENT_WRITE
            if not mask:
                self._selector.unregister(fd)
            else:
                self._selector.modify(fd, mask, (reader, None))

            if writer is not None:
                writer.cancel()
                return True
            else:
                return False 
開發者ID:CedricGuillemet,項目名稱:Imogen,代碼行數:24,代碼來源:selector_events.py

示例7: service_connection

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def service_connection(key, mask):
    sock = key.fileobj
    data = key.data
    if mask & selectors.EVENT_READ:
        recv_data = sock.recv(BUFFER_SIZE)
        if recv_data:
            data.outb += recv_data
        else:
            print('Closing connection in {}'.format(data.addr))
            selector.unregister(sock)
            sock.close()
    if mask & selectors.EVENT_WRITE:
        if data.outb:
            print('Echo from {} to {}'.format(repr(data.outb), data.addr))
            sent = sock.send(data.outb)
            data.outb = data.outb[sent:] 
開發者ID:PacktPublishing,項目名稱:Learning-Python-Networking-Second-Edition,代碼行數:18,代碼來源:tcp_server_selectors.py

示例8: start_connections

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def start_connections(host, port, num_conns):
    server_address = (host, port)
    for i in range(0, num_conns):
        connid = i + 1
        print('Starting connection {} towards {}'.format(connid, server_address))
        socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # We connect using connect_ex () instead of connect ()
        socket_tcp.connect_ex(server_address)
        events = selectors.EVENT_READ | selectors.EVENT_WRITE
        data = types.SimpleNamespace(connid=connid,
                                     msg_total=sum(len(m) for m in messages),
                                     recv_total=0,
                                     messages=list(messages),
                                     outb=b'')
        selector.register(socket_tcp, events, data=data)
    events = selector.select()
    for key, mask in events:
        service_connection(key, mask) 
開發者ID:PacktPublishing,項目名稱:Learning-Python-Networking-Second-Edition,代碼行數:20,代碼來源:tcp_client_selectors.py

示例9: service_connection

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def service_connection(key, mask):
    sock = key.fileobj
    data = key.data
    if mask & selectors.EVENT_READ:
        recv_data = sock.recv(BUFFER_SIZE)
        if recv_data:
            print('Received {} from connection {}'.format(repr(recv_data), data.connid))
            data.recv_total += len(recv_data)
        if not recv_data or data.recv_total == data.msg_total:
            print('Closing connection', data.connid)
            selector.unregister(sock)
            sock.close()
    if mask & selectors.EVENT_WRITE:
        if not data.outb and data.messages:
            data.outb = data.messages.pop(0).encode()
        if data.outb:
            print('Sending {} to connection {}'.format(repr(data.outb), data.connid))
            sent = sock.send(data.outb)
            sock.shutdown(socket.SHUT_WR)
            data.outb = data.outb[sent:] 
開發者ID:PacktPublishing,項目名稱:Learning-Python-Networking-Second-Edition,代碼行數:22,代碼來源:tcp_client_selectors.py

示例10: test_select

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def test_select(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)

        rd, wr = self.make_socketpair()

        s.register(rd, selectors.EVENT_READ)
        wr_key = s.register(wr, selectors.EVENT_WRITE)

        result = s.select()
        for key, events in result:
            self.assertTrue(isinstance(key, selectors.SelectorKey))
            self.assertTrue(events)
            self.assertFalse(events & ~(selectors.EVENT_READ |
                                        selectors.EVENT_WRITE))

        self.assertEqual([(wr_key, selectors.EVENT_WRITE)], result) 
開發者ID:ShikyoKira,項目名稱:Project-New-Reign---Nemesis-Main,代碼行數:19,代碼來源:test_selectors.py

示例11: _wr_shutdown

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def _wr_shutdown(self, conn, once=False):
        """action when connection should be write-shutdown
        :type conn: socket.socket
        """
        try:
            conn.shutdown(socket.SHUT_WR)
        except:
            pass

        if conn in self.conn_wr:
            self.conn_wr.remove(conn)
            if self.sel:
                self._sel_disable_event(conn, EVENT_WRITE)

        if not once and conn in self.map:  # use the `once` param to avoid infinite loop
            #   pair should be rd_shutdown.
            # if a socket is wr_shutdowned, then it's
            self._rd_shutdown(self.map[conn], True) 
開發者ID:aploium,項目名稱:shootback,代碼行數:20,代碼來源:common_func.py

示例12: test_add_reader_existing

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def test_add_reader_existing(self):
        reader = mock.Mock()
        writer = mock.Mock()
        self.loop._selector.get_key.return_value = selectors.SelectorKey(
            1, 1, selectors.EVENT_WRITE, (reader, writer))
        cb = lambda: True
        self.loop.add_reader(1, cb)

        self.assertTrue(reader.cancel.called)
        self.assertFalse(self.loop._selector.register.called)
        self.assertTrue(self.loop._selector.modify.called)
        fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
        self.assertEqual(1, fd)
        self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
        self.assertEqual(cb, r._callback)
        self.assertEqual(writer, w) 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:18,代碼來源:test_selector_events.py

示例13: test_add_writer_existing

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def test_add_writer_existing(self):
        reader = mock.Mock()
        writer = mock.Mock()
        self.loop._selector.get_key.return_value = selectors.SelectorKey(
            1, 1, selectors.EVENT_READ, (reader, writer))
        cb = lambda: True
        self.loop.add_writer(1, cb)

        self.assertTrue(writer.cancel.called)
        self.assertFalse(self.loop._selector.register.called)
        self.assertTrue(self.loop._selector.modify.called)
        fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
        self.assertEqual(1, fd)
        self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
        self.assertEqual(reader, r)
        self.assertEqual(cb, w._callback) 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:18,代碼來源:test_selector_events.py

示例14: __on_write_activated

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def __on_write_activated(self, fd):
        self._logger.debug('File {} ready to write'.format(fd))
        key = self._key_from_fd(fd)
        if key:
            self.__parent._process_event(key, EVENT_WRITE & key.events) 
開發者ID:gmarull,項目名稱:asyncqt,代碼行數:7,代碼來源:_unix.py

示例15: test_unregister_after_fd_close

# 需要導入模塊: import selectors [as 別名]
# 或者: from selectors import EVENT_WRITE [as 別名]
def test_unregister_after_fd_close(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)
        rd, wr = self.make_socketpair()
        r, w = rd.fileno(), wr.fileno()
        s.register(r, selectors.EVENT_READ)
        s.register(w, selectors.EVENT_WRITE)
        rd.close()
        wr.close()
        s.unregister(r)
        s.unregister(w) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:13,代碼來源:test_selectors.py


注:本文中的selectors.EVENT_WRITE屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。