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)
python类poll()的实例源码
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)
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)
def receive_line(self, timeout = 0):
line = self.socket.readline()
if line:
try:
msg = kjson.loads(line.rstrip())
except:
raise Exception('invalid message from server:', line)
return msg
if timeout < 0:
return False
t = time.time()
try:
if not self.poll(timeout):
return False
except ConnectionLost:
self.disconnected()
dt = time.time()-t
return self.receive_line(timeout - dt)
def flush(self):
if not self.out_buffer:
return
try:
if not self.pollout.poll(0):
if sendfail_cnt >= sendfail_msg:
print 'signalk socket failed to send', sendfail_cnt
self.sendfail_msg *= 10
self.sendfail_cnt += 1
return
t0 = time.time()
count = self.socket.send(self.out_buffer)
t1 = time.time()
if t1-t0 > .1:
print 'socket send took too long!?!?', t1-t0
if count < 0:
print 'socket send error', count
self.socket.close()
self.out_buffer = self.out_buffer[count:]
except:
self.socket.close()
def HandleRequests(self):
if not self.init:
try:
self.server_socket.bind(('0.0.0.0', self.port))
except:
print 'signalk_server: bind failed, try again.'
time.sleep(1)
return
self.server_socket.listen(5)
self.init = True
self.fd_to_socket = {self.server_socket.fileno() : self.server_socket}
self.poller = select.poll()
self.poller.register(self.server_socket, select.POLLIN)
t1 = time.time()
if t1 >= self.persistent_timeout:
self.StorePersistentValues()
if time.time() - t1 > .1:
print 'persistent store took too long!', time.time() - t1
return
self.PollSockets()
def initialize(self):
cnt = 0
data = False
while self.flags & ServoFlags.OVERCURRENT or \
not self.flags & ServoFlags.SYNC:
self.stop()
if self.poll():
data = True
time.sleep(.001)
cnt+=1
if cnt == 400 and not data:
return False
if cnt == 1000:
return False
return True
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)
def once(self):
# check if there is a request to get and clear accumulated logs
msg = self._recv(timeout=0)
if msg == 'get':
self._send(self.log_buf.getvalue())
self.log_buf = StringIO()
# is there new logcat output to read?
if not self.poller.poll(500): # millisecond timeout
return
# read logcat output and store it in a buffer, optionally to file too
log = os.read(self.cmd_fd, 1000)
if self.log_file:
self.log_file.write(log)
self.log_file.flush()
if self.log_buf.tell() > LOGBUF_MAXSIZE:
self.log_buf = StringIO() # start over to avoid OOM
self.log_buf.write(log)
def once(self):
# check if there is a request to get and clear accumulated logs
msg = self._recv(timeout=0)
if msg == 'get':
self._send(self.log_buf.getvalue())
self.log_buf = StringIO()
# is there new logcat output to read?
if not self.poller.poll(500): # millisecond timeout
return
# read logcat output and store it in a buffer, optionally to file too
log = os.read(self.cmd_fd, 1000)
if self.log_file:
self.log_file.write(log)
self.log_file.flush()
if self.log_buf.tell() > LOGBUF_MAXSIZE:
self.log_buf = StringIO() # start over to avoid OOM
self.log_buf.write(log)
def t4():
pretty = '%s t4' % __file__
print(pretty)
pid, fd = ave.cmd.run_bg('echo hello')
poller = select.poll()
poller.register(fd, select.POLLIN)
events = poller.poll(1000) # milliseconds
tmp = ''
for e in events:
if not (e[1] & select.POLLIN):
print('FAIL %s: unexpected poll event: %d' % (pretty, e[1]))
os.kill(pid, signal.SIGKILL)
tmp += os.read(fd, 1024)
if not tmp.startswith('hello'):
print('FAIL %s: wrong result: "%s"' % (pretty, tmp))
os.kill(pid, signal.SIGKILL)
os.waitpid(pid, 0)
return True
# check that return value from executed program is correct
def write(self, payload, timeout=None):
size = len(payload)
done = 0
limit = None
if timeout != None: # first and last test of 'timeout'
limit = time.time() + timeout
while done < size:
try:
done += Connection.write(self, payload[done:])
except ConnectionAgain:
if limit:
timeout == limit - time.time()
else:
timeout = -1
events = self.poll(select.POLLOUT, timeout)
if not events:
raise ConnectionTimeout('write attempt timed out')
if events[0][1] & (select.POLLHUP | select.POLLERR):
raise ConnectionClosed(
'write attempt failed with %d at %d %f'
% (events[0][1], done, timeout)
)
if events[0][1] & select.POLLOUT:
continue
raise Exception('unknown events: %s' % events)
def read(self, size, timeout=None):
if timeout != None:
limit = time.time() + timeout
payload = ''
while len(payload) < size:
if timeout != None:
events = self.poll(select.POLLIN, max(0, limit - time.time()))
else:
events = self.poll(select.POLLIN, -1)
if not events:
raise ConnectionTimeout()
if events[0][1] & ERRMASK:
raise ConnectionReset()
tmp = os.read(self.r, size)
if not tmp:
raise ConnectionClosed()
payload += tmp
return tmp
def write(self, payload, timeout=None):
size = len(payload)
done = 0
limit = None
if timeout != None: # first and last test of 'timeout'
limit = time.time() + timeout
while done < size:
try:
done += Connection.write(self, payload[done:])
except ConnectionAgain:
if limit:
timeout == limit - time.time()
else:
timeout = -1
events = self.poll(select.POLLOUT, timeout)
if not events:
raise ConnectionTimeout('write attempt timed out')
if events[0][1] & (select.POLLHUP | select.POLLERR):
raise ConnectionClosed(
'write attempt failed with %d at %d %f'
% (events[0][1], done, timeout)
)
if events[0][1] & select.POLLOUT:
continue
raise Exception('unknown events: %s' % events)
def read(self, size, timeout=None):
if timeout != None:
limit = time.time() + timeout
payload = ''
while len(payload) < size:
if timeout != None:
events = self.poll(select.POLLIN, max(0, limit - time.time()))
else:
events = self.poll(select.POLLIN, -1)
if not events:
raise ConnectionTimeout()
if events[0][1] & ERRMASK:
raise ConnectionReset()
tmp = os.read(self.r, size)
if not tmp:
raise ConnectionClosed()
payload += tmp
return tmp
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)
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)
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
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 = math.ceil(timeout * 1e3)
ready = []
try:
fd_event_list = 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
def select(self, timeout=None):
if timeout is None:
timeout = None
elif timeout <= 0:
timeout = 0
else:
# devpoll() has a resolution of 1 millisecond, round away from
# zero to wait *at least* timeout seconds.
timeout = math.ceil(timeout * 1e3)
ready = []
try:
fd_event_list = self._devpoll.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
def select(self, timeout=None):
if timeout is not None:
if timeout <= 0:
timeout = 0.0
else:
# select.epoll.poll() has a resolution of 1 millisecond
# but luckily takes seconds so we don't need a wrapper
# like PollSelector. Just for better rounding.
timeout = math.ceil(timeout * 1e3) * 1e-3
timeout = float(timeout)
else:
timeout = -1.0 # epoll.poll() must have a float.
# We always want at least 1 to ensure that select can be called
# with no file descriptors registered. Otherwise will fail.
max_events = max(len(self._fd_to_key), 1)
ready = []
fd_events = _syscall_wrapper(self._epoll.poll, True,
timeout=timeout,
maxevents=max_events)
for fd, event_mask in fd_events:
events = 0
if event_mask & ~select.EPOLLIN:
events |= EVENT_WRITE
if event_mask & ~select.EPOLLOUT:
events |= EVENT_READ
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
def __init__ (self,bb,cal_mod,BB_X,BB_Y):
threading.Thread.__init__(self)
self.runflag = True
self.storeflag = False
self.n_s = 4
self.bbdev = xwiimote.iface(bb.sys_path)
self.p = select.poll()
self.p.register(self.bbdev.get_fd(), select.POLLIN)
# open bb device
self.bbdev.open(xwiimote.IFACE_BALANCE_BOARD)
# create xwiimote event structure
self.revt = xwiimote.event()
# create numpy array to store data from board
self.tmp_dat = np.empty((1,self.n_s))
self.cop = np.empty((1,2))
self.cop_dat = np.empty((0,2))
self.cal_mod = cal_mod
self.BB_X = BB_X
self.BB_Y = BB_Y
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)
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)
def poll(self, timeout):
rlist, wlist, xlist = select.select(list(self.__descrsRead),
list(self.__descrsWrite),
list(self.__descrsError),
timeout)
allDescrs = set(rlist + wlist + xlist)
rlist = set(rlist)
wlist = set(wlist)
xlist = set(xlist)
for descr in allDescrs:
event = 0
if descr in rlist:
event |= POLL_EVENT_TYPE.READ
if descr in wlist:
event |= POLL_EVENT_TYPE.WRITE
if descr in xlist:
event |= POLL_EVENT_TYPE.ERROR
self.__descrToCallbacks[descr](descr, event)
def __init__(self, iocp_notifier):
self._poller_name = 'select'
self._fds = {}
self._events = {}
self._terminate = False
self.rset = set()
self.wset = set()
self.xset = set()
self.iocp_notifier = iocp_notifier
self.cmd_rsock, self.cmd_wsock = _AsyncPoller._socketpair()
self.cmd_rsock.setblocking(0)
self.cmd_wsock.setblocking(0)
self.poller = select.select
self._polling = False
self._lock = threading.RLock()
self.poll_thread = threading.Thread(target=self.poll)
self.poll_thread.daemon = True
self.poll_thread.start()
def poll(self, timeout):
rlist, wlist, xlist = self.poller(self.rset, self.wset, self.xset, timeout)
events = {}
for fid in rlist:
events[fid] = _AsyncPoller._Read
for fid in wlist:
events[fid] = events.get(fid, 0) | _AsyncPoller._Write
for fid in xlist:
events[fid] = events.get(fid, 0) | _AsyncPoller._Error
return events.iteritems()
def __init__(self, iocp_notifier):
self._poller_name = 'select'
self._fds = {}
self._events = {}
self._terminate = False
self.rset = set()
self.wset = set()
self.xset = set()
self.iocp_notifier = iocp_notifier
self.cmd_rsock, self.cmd_wsock = _AsyncPoller._socketpair()
self.cmd_rsock.setblocking(0)
self.cmd_wsock.setblocking(0)
self.poller = select.select
self._polling = False
self._lock = threading.RLock()
self.poll_thread = threading.Thread(target=self.poll)
self.poll_thread.daemon = True
self.poll_thread.start()
def poll(self, timeout):
kevents = self.poller.control(None, 500, timeout)
events = [(kevent.ident,
_AsyncPoller._Read if kevent.filter == select.KQ_FILTER_READ else
_AsyncPoller._Write if kevent.filter == select.KQ_FILTER_WRITE else
_AsyncPoller._Hangup if kevent.flags == select.KQ_EV_EOF else
_AsyncPoller._Error if kevent.flags == select.KQ_EV_ERROR else 0)
for kevent in kevents]
return events
def poll(self, timeout):
rlist, wlist, xlist = self.poller(self.rset, self.wset, self.xset, timeout)
events = {}
for fid in rlist:
events[fid] = _AsyncPoller._Read
for fid in wlist:
events[fid] = events.get(fid, 0) | _AsyncPoller._Write
for fid in xlist:
events[fid] = events.get(fid, 0) | _AsyncPoller._Error
return events.items()