本文整理汇总了Python中select.poll方法的典型用法代码示例。如果您正苦于以下问题:Python select.poll方法的具体用法?Python select.poll怎么用?Python select.poll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类select
的用法示例。
在下文中一共展示了select.poll方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: for_events
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def for_events(cls, *events):
"""Listen for ``events``.
``events`` is a list of ``(fd, event)`` pairs, where ``fd`` is a file
descriptor or file object and ``event`` either ``'r'`` or ``'w'``. If
``r``, listen for whether that is ready to be read. If ``w``, listen
for whether the channel is ready to be written to.
"""
notifier = eintr_retry_call(select.poll)
for fd, event in events:
mask = cls._EVENT_TO_MASK.get(event)
if not mask:
raise ValueError('Unknown event type: {0!r}'.format(event))
notifier.register(fd, mask)
return cls(notifier)
示例2: poll
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def poll(self, timeout=None):
"""Poll for events.
``timeout`` is an integer specifying how long to wait for events (in
milliseconds). If omitted, ``None`` or negative, wait until an event
occurs.
Return a list of all events that occurred before ``timeout``, where
each event is a pair ``(fd, event)``. ``fd`` is the integral file
descriptor, and ``event`` a string indicating the event type. If
``'r'``, there is data to read from ``fd``. If ``'w'``, ``fd`` is
writable without blocking now. If ``'h'``, the file descriptor was
hung up (i.e. the remote side of a pipe was closed).
"""
# Return a list to allow clients to determine whether there are any
# events at all with a simple truthiness test.
return list(self._parse_events(eintr_retry_call(self._notifier.poll, timeout)))
示例3: _parse_events
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def _parse_events(self, events):
"""Parse ``events``.
``events`` is a list of events as returned by
:meth:`select.poll.poll()`.
Yield all parsed events.
"""
for fd, event_mask in events:
if self._has_event(event_mask, select.POLLNVAL):
raise IOError('File descriptor not open: {0!r}'.format(fd))
elif self._has_event(event_mask, select.POLLERR):
raise IOError('Error while polling fd: {0!r}'.format(fd))
if self._has_event(event_mask, select.POLLIN):
yield fd, 'r'
if self._has_event(event_mask, select.POLLOUT):
yield fd, 'w'
if self._has_event(event_mask, select.POLLHUP):
yield fd, 'h'
示例4: deliver_dnotify
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def deliver_dnotify(self, dnstring, _recurse = 0):
if self.s == None:
self.connect()
if _recurse > _MAX_RECURSE:
raise Exception('Cannot reconnect: %s', self.spath)
if not dnstring.endswith('\n'):
dnstring += '\n'
while True:
try:
self.s.send(dnstring)
break
except socket.error as why:
if why[0] == EINTR:
continue
elif why[0] in (EPIPE, ENOTCONN, ECONNRESET):
self.s = None
return self.deliver_dnotify(dnstring, _recurse + 1)
raise why
# Clean any incoming data on the socket
if len(self.poller.poll(0)) > 0:
try:
self.s.recv(1024)
except:
pass
return
示例5: run
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def run(self):
#print(self.run, 'enter')
while True:
#print(self.run, 'cycle')
pollret = dict(self.pollobj.poll()).get(self.fileno, 0)
if pollret & POLLNVAL != 0:
break
if pollret & POLLIN == 0:
continue
try:
clientsock, addr = self.clicm.serversock.accept()
except Exception as why:
if isinstance(why, socket.error):
if why.errno == ECONNABORTED:
continue
elif why.errno == EBADF:
break
else:
raise
dump_exception('CLIConnectionManager: unhandled exception when accepting incoming connection')
break
#print(self.run, 'handle_accept')
ED2.callFromThread(self.clicm.handle_accept, clientsock, addr)
self.clicm = None
#print(self.run, 'exit')
示例6: poll_wait_for_socket
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def poll_wait_for_socket(sock, read=False, write=False, timeout=None):
if not read and not write:
raise RuntimeError("must specify at least one of read=True, write=True")
mask = 0
if read:
mask |= select.POLLIN
if write:
mask |= select.POLLOUT
poll_obj = select.poll()
poll_obj.register(sock, mask)
# For some reason, poll() takes timeout in milliseconds
def do_poll(t):
if t is not None:
t *= 1000
return poll_obj.poll(t)
return bool(_retry_on_intr(do_poll, timeout))
示例7: _can_allocate
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def _can_allocate(struct):
"""Checks that select structs can be allocated by the underlying operating system.
Otherwise it could be just advertised by the select module. We don't check select() because we'll be hopeful
that most platforms that don't have it available will not advertise it. (ie: GAE).
"""
try:
# select.poll() objects won't fail until used.
if struct == 'poll':
p = select.poll()
p.poll(0)
# All others will fail on allocation.
else:
getattr(select, struct)().close()
return True
except (OSError, AttributeError):
return False
# Choose the best implementation, roughly:
# kqueue == epoll > poll > select. Devpoll not supported. (See above)
# select() also can't accept a FD > FD_SETSIZE (usually around 1024)
示例8: _can_allocate
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def _can_allocate(struct):
""" Checks that select structs can be allocated by the underlying
operating system, not just advertised by the select module. We don't
check select() because we'll be hopeful that most platforms that
don't have it available will not advertise it. (ie: GAE) """
try:
# select.poll() objects won't fail until used.
if struct == 'poll':
p = select.poll()
p.poll(0)
# All others will fail on allocation.
else:
getattr(select, struct)().close()
return True
except (OSError, AttributeError) as e:
return False
# Choose the best implementation, roughly:
# kqueue == epoll > poll > select. Devpoll not supported. (See above)
# select() also can't accept a FD > FD_SETSIZE (usually around 1024)
示例9: _select
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def _select(self):
with self._lock:
fds = self._file_descriptors
fd_to_callback = self._file_descriptor_to_callback
if fds:
if _HAS_POLL:
# With 100 file descriptors, it is approximately 5x slower to
# recreate and reinitialize the Poll object on every call to _select
# rather reuse one. But the absolute cost of contruction,
# initialization and calling poll(0) is ~25us so code simplicity
# wins.
poll = select.poll()
for fd in fds:
poll.register(fd, select.POLLIN)
ready_file_descriptors = [fd for fd, _ in poll.poll(1)]
else:
ready_file_descriptors, _, _ = select.select(fds, [], [], 1)
for fd in ready_file_descriptors:
fd_to_callback[fd]()
else:
# select([], [], [], 1) is not supported on Windows.
time.sleep(1)
示例10: select
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def select(self, timeout=None):
if timeout is None:
timeout = None
elif timeout <= 0:
timeout = 0
else:
# poll() has a resolution of 1 millisecond, round away from
# zero to wait *at least* timeout seconds.
timeout = int(math.ceil(timeout * 1e3))
ready = []
try:
fd_event_list = wrap_error(self._poll.poll, timeout)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & ~select.POLLIN:
events |= EVENT_WRITE
if event & ~select.POLLOUT:
events |= EVENT_READ
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
示例11: __init__
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def __init__(self, host=None, port=0,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
"""Constructor.
When called without arguments, create an unconnected instance.
With a hostname argument, it connects the instance; port number
and timeout are optional.
"""
self.debuglevel = DEBUGLEVEL
self.host = host
self.port = port
self.timeout = timeout
self.sock = None
self.rawq = ''
self.irawq = 0
self.cookedq = ''
self.eof = 0
self.iacseq = '' # Buffer for IAC sequence.
self.sb = 0 # flag for SB and SE sequence.
self.sbdataq = ''
self.option_callback = None
self._has_poll = hasattr(select, 'poll')
if host is not None:
self.open(host, port, timeout)
示例12: loop
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def loop(timeout=30.0, use_poll=False, map=None, count=None):
if map is None:
map = socket_map
if use_poll and hasattr(select, 'poll'):
poll_fun = poll2
else:
poll_fun = poll
if count is None:
while map:
poll_fun(timeout, map)
else:
while map and count > 0:
poll_fun(timeout, map)
count = count - 1
示例13: __init__
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def __init__(self, notifier):
"""Create a poll object for the given ``notifier``.
``notifier`` is the :class:`select.poll` object wrapped by the new poll
object.
"""
self._notifier = notifier
示例14: connect
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def connect(self):
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.s.connect(self.spath)
self.poller = select.poll()
self.poller.register(self.s, select.POLLIN)
示例15: __init__
# 需要导入模块: import select [as 别名]
# 或者: from select import poll [as 别名]
def __init__(self, clicm):
Thread.__init__(self)
self.clicm = clicm
self.pollobj = poll()
self.fileno = self.clicm.serversock.fileno()
self.pollobj.register(self.fileno, POLLIN)
self.setDaemon(True)
self.start()