Python uselect.poll() Examples
The following are 24
code examples of uselect.poll().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
uselect
, or try the search function
.
Example #1
Source File: main.py From uPyEcho with Apache License 2.0 | 7 votes |
def poll(self, timeout=100): if self.use_poll: ready = self.poller.poll(timeout) else: ready = [] if len(self.targets) > 0: (rlist, wlist, xlist) = select.select( self.targets.keys(), [], [], timeout ) ready = [(x, None) for x in rlist] for one_ready in ready: target = self.targets.get(one_ready[0].fileno(), None) dbg("Targets %s" % str(self.targets.keys())) if target: # dbg("get socket with fileno: %s" % str(one_ready[0].fileno()) + " len: %s" % len(one_ready) + " selected: %s " % str(target.fileno()) ) # update time target.do_read(one_ready[0])
Example #2
Source File: __init__.py From uPyCam with Apache License 2.0 | 6 votes |
def wait(self, delay): if DEBUG and __debug__: log.debug("poll.wait(%d)", delay) # We need one-shot behavior (second arg of 1 to .poll()) res = self.poller.ipoll(delay, 1) #log.debug("poll result: %s", res) # Remove "if res" workaround after # https://github.com/micropython/micropython/issues/2716 fixed. if res: for sock, ev in res: cb = self.objmap[id(sock)] if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_reader(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: cb.pend_throw(None) self.call_soon(cb)
Example #3
Source File: __init__.py From microhomie with MIT License | 6 votes |
def wait(self, delay): if DEBUG and __debug__: log.debug("poll.wait(%d)", delay) # We need one-shot behavior (second arg of 1 to .poll()) res = self.poller.ipoll(delay, 1) #log.debug("poll result: %s", res) # Remove "if res" workaround after # https://github.com/micropython/micropython/issues/2716 fixed. if res: for sock, ev in res: cb = self.objmap[id(sock)] if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_reader(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: cb.pend_throw(None) self.call_soon(cb)
Example #4
Source File: userver.py From micropython-async with MIT License | 6 votes |
def run(self, loop, port=8123): addr = socket.getaddrinfo('0.0.0.0', port, 0, socket.SOCK_STREAM)[0][-1] s_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # server socket s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s_sock.bind(addr) s_sock.listen(5) self.socks = [s_sock] # List of current sockets for .close() print('Awaiting connection on port', port) poller = select.poll() poller.register(s_sock, select.POLLIN) client_id = 1 # For user feedback while True: res = poller.poll(1) # 1ms block if res: # Only s_sock is polled c_sock, _ = s_sock.accept() # get client socket loop.create_task(self.run_client(c_sock, client_id)) client_id += 1 await asyncio.sleep_ms(200)
Example #5
Source File: __init__.py From pysmartnode with MIT License | 6 votes |
def wait(self, delay): if DEBUG and __debug__: log.debug("poll.wait(%d)", delay) # We need one-shot behavior (second arg of 1 to .poll()) res = self.poller.ipoll(delay, 1) # log.debug("poll result: %s", res) # Remove "if res" workaround after # https://github.com/micropython/micropython/issues/2716 fixed. if res: for sock, ev in res: cb = self.objmap[id(sock)] if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_reader(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: cb.pend_throw(None) self.call_soon(cb)
Example #6
Source File: ws_connection.py From upy-websocket-server with MIT License | 6 votes |
def read(self): poll_events = self.poll.poll(0) if not poll_events: return # Check the flag for connection hung up if poll_events[0][1] & uselect.POLLHUP: self.client_close = True msg_bytes = None try: msg_bytes = self.ws.read() except OSError: self.client_close = True # If no bytes => connection closed. See the link below. # http://stefan.buettcher.org/cs/conn_closed.html if not msg_bytes or self.client_close: raise ClientClosedError() return msg_bytes
Example #7
Source File: __init__.py From microdot with MIT License | 6 votes |
def wait(self, delay): if DEBUG and __debug__: log.debug("poll.wait(%d)", delay) # We need one-shot behavior (second arg of 1 to .poll()) res = self.poller.ipoll(delay, 1) #log.debug("poll result: %s", res) # Remove "if res" workaround after # https://github.com/micropython/micropython/issues/2716 fixed. if res: for sock, ev in res: cb = self.objmap[id(sock)] if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_reader(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: cb.pend_throw(None) self.call_soon(cb)
Example #8
Source File: client_w.py From micropython-samples with MIT License | 6 votes |
def run(): global success ok = True try: while ok: res = poller.ipoll(10) for sock, ev in res: if ev & select.POLLOUT: r = sock.send(b'0123456789\n') print(ev, r) # On ESP8266 if another task closes the socket the poll object # never triggers. uasyncio expects it to trigger with POLLHUP or # (POLLOUT & POLLERR or POLLOUT & POLLHUP) # If server fails gets OSError on both platforms. else: # But on Unix socket closure produces ev == 32 print('Terminating event:', ev) # What is 32?? ok = False break await asyncio.sleep(1) await asyncio.sleep(0) except OSError: print('Got OSError') # Happens on ESP8266 if server fails success = True # Detected socket closure or error by OSError or event
Example #9
Source File: __init__.py From micropython-samples with MIT License | 6 votes |
def wait(self, delay): if DEBUG and __debug__: log.debug("poll.wait(%d)", delay) # We need one-shot behavior (second arg of 1 to .poll()) res = self.poller.ipoll(delay, 1) #log.debug("poll result: %s", res) # Remove "if res" workaround after # https://github.com/micropython/micropython/issues/2716 fixed. if res: for sock, ev in res: cb = self.objmap[id(sock)] if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_reader(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: cb.pend_throw(None) self.call_soon(cb)
Example #10
Source File: __init__.py From pysmartnode with MIT License | 5 votes |
def __init__(self, runq_len=16, waitq_len=16): EventLoop.__init__(self, runq_len, waitq_len) self.poller = select.poll() self.objmap = {}
Example #11
Source File: __init__.py From uPyCam with Apache License 2.0 | 5 votes |
def __init__(self, runq_len=16, waitq_len=16): EventLoop.__init__(self, runq_len, waitq_len) self.poller = select.poll() self.objmap = {}
Example #12
Source File: __init__.py From microhomie with MIT License | 5 votes |
def __init__(self, runq_len=16, waitq_len=16): EventLoop.__init__(self, runq_len, waitq_len) self.poller = select.poll() self.objmap = {}
Example #13
Source File: blynklib_mp.py From lib-python with MIT License | 5 votes |
def _set_socket_timeout(self, timeout): if getattr(self._socket, 'settimeout', None): self._socket.settimeout(timeout) else: p = select.poll() p.register(self._socket) p.poll(int(timeout * const(1000)))
Example #14
Source File: __init__.py From micropython-async with MIT License | 5 votes |
def read(self, n=-1): while True: yield IORead(self.polls) res = self.ios.read(n) # Call the device's read method if res is not None: break # This should not happen for real sockets, but can easily # happen for stream wrappers (ssl, websockets, etc.) #log.warn("Empty read") yield IOReadDone(self.polls) # uasyncio.core calls remove_reader # This de-registers device as a read device with poll via # PollEventLoop._unregister return res # Next iteration raises StopIteration and returns result
Example #15
Source File: __init__.py From micropython-async with MIT License | 5 votes |
def __init__(self, runq_len=16, waitq_len=16, fast_io=0, lp_len=0): EventLoop.__init__(self, runq_len, waitq_len, fast_io, lp_len) self.poller = select.poll() self.rdobjmap = {} self.wrobjmap = {} self.flags = {} # Remove registration of sock for reading or writing.
Example #16
Source File: main.py From uPyEcho with Apache License 2.0 | 5 votes |
def __init__(self): if "poll" in dir(select): self.use_poll = True self.poller = select.poll() else: self.use_poll = False self.targets = {}
Example #17
Source File: server.py From micropython-iot with MIT License | 5 votes |
def run(expected, verbose=False, port=8123, timeout=2000): addr = socket.getaddrinfo('0.0.0.0', port, 0, socket.SOCK_STREAM)[0][-1] s_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # server socket s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s_sock.bind(addr) s_sock.listen(len(expected) + 2) verbose and print('Awaiting connection.', port) poller = select.poll() poller.register(s_sock, select.POLLIN) to_secs = timeout / 1000 # ms -> secs while True: res = poller.poll(1) # 1ms block if res: # Only s_sock is polled c_sock, _ = s_sock.accept() # get client socket c_sock.setblocking(False) try: data = await _readid(c_sock, to_secs) except OSError: c_sock.close() else: Connection.go(to_secs, data, verbose, c_sock, s_sock, expected) await asyncio.sleep(0.2) # A Connection persists even if client dies (minimise object creation). # If client dies Connection is closed: ._close() flags this state by closing its # socket and setting .sock to None (.status() == False).
Example #18
Source File: ws_server.py From upy-websocket-server with MIT License | 5 votes |
def _check_new_connections(self, accept_handler): poll_events = self._listen_poll.poll(0) if not poll_events: return if poll_events[0][1] & uselect.POLLIN: accept_handler()
Example #19
Source File: ws_server.py From upy-websocket-server with MIT License | 5 votes |
def _setup_conn(self, port): self._listen_s = socket.socket() self._listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._listen_poll = uselect.poll() ai = socket.getaddrinfo("0.0.0.0", port) addr = ai[0][4] self._listen_s.bind(addr) self._listen_s.listen(1) self._listen_poll.register(self._listen_s) for i in (network.AP_IF, network.STA_IF): iface = network.WLAN(i) if iface.active(): print("WebSocket started on ws://%s:%d" % (iface.ifconfig()[0], port))
Example #20
Source File: ws_connection.py From upy-websocket-server with MIT License | 5 votes |
def close(self): print("Closing connection.") self.poll.unregister(self.socket) self.socket.close() self.socket = None self.ws = None if self.close_callback: self.close_callback(self)
Example #21
Source File: ws_connection.py From upy-websocket-server with MIT License | 5 votes |
def __init__(self, addr, s, close_callback): self.client_close = False self._need_check = False self.address = addr self.socket = s self.ws = websocket(s, True) self.poll = uselect.poll() self.close_callback = close_callback self.socket.setblocking(False) self.poll.register(self.socket, uselect.POLLIN)
Example #22
Source File: __init__.py From micropython-samples with MIT License | 5 votes |
def __init__(self, runq_len=16, waitq_len=16): EventLoop.__init__(self, runq_len, waitq_len) self.poller = select.poll() self.objmap = {}
Example #23
Source File: __init__.py From microdot with MIT License | 5 votes |
def __init__(self, runq_len=16, waitq_len=16): EventLoop.__init__(self, runq_len, waitq_len) self.poller = select.poll() self.objmap = {}
Example #24
Source File: __init__.py From micropython-async with MIT License | 4 votes |
def wait(self, delay): if DEBUG and __debug__: log.debug("poll.wait(%d)", delay) # We need one-shot behavior (second arg of 1 to .poll()) res = self.poller.ipoll(delay, 1) #log.debug("poll result: %s", res) for sock, ev in res: if ev & select.POLLOUT: cb = self.wrobjmap[id(sock)] if cb is None: continue # Not yet ready. # Invalidate objmap: can get adverse timing in fast_io whereby add_writer # is not called soon enough. Ignore poll events occurring before we are # ready to handle them. self.wrobjmap[id(sock)] = None if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_writer(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: prev = cb.pend_throw(None) # Enable task to run. #if isinstance(prev, Exception): #print('Put back exception') #cb.pend_throw(prev) self._call_io(cb) # Put coro onto runq (or ioq if one exists) if ev & select.POLLIN: cb = self.rdobjmap[id(sock)] if cb is None: continue self.rdobjmap[id(sock)] = None if ev & (select.POLLHUP | select.POLLERR): # These events are returned even if not requested, and # are sticky, i.e. will be returned again and again. # If the caller doesn't do proper error handling and # unregister this sock, we'll busy-loop on it, so we # as well can unregister it now "just in case". self.remove_reader(sock) if DEBUG and __debug__: log.debug("Calling IO callback: %r", cb) if isinstance(cb, tuple): cb[0](*cb[1]) else: prev = cb.pend_throw(None) # Enable task to run. #if isinstance(prev, Exception): #cb.pend_throw(prev) #print('Put back exception') self._call_io(cb)