OLD | NEW |
(Empty) | |
| 1 from .selectors import ( |
| 2 HAS_SELECT, |
| 3 DefaultSelector, |
| 4 EVENT_READ, |
| 5 EVENT_WRITE |
| 6 ) |
| 7 |
| 8 |
| 9 def _wait_for_io_events(socks, events, timeout=None): |
| 10 """ Waits for IO events to be available from a list of sockets |
| 11 or optionally a single socket if passed in. Returns a list of |
| 12 sockets that can be interacted with immediately. """ |
| 13 if not HAS_SELECT: |
| 14 raise ValueError('Platform does not have a selector') |
| 15 if not isinstance(socks, list): |
| 16 # Probably just a single socket. |
| 17 if hasattr(socks, "fileno"): |
| 18 socks = [socks] |
| 19 # Otherwise it might be a non-list iterable. |
| 20 else: |
| 21 socks = list(socks) |
| 22 with DefaultSelector() as selector: |
| 23 for sock in socks: |
| 24 selector.register(sock, events) |
| 25 return [key[0].fileobj for key in |
| 26 selector.select(timeout) if key[1] & events] |
| 27 |
| 28 |
| 29 def wait_for_read(socks, timeout=None): |
| 30 """ Waits for reading to be available from a list of sockets |
| 31 or optionally a single socket if passed in. Returns a list of |
| 32 sockets that can be read from immediately. """ |
| 33 return _wait_for_io_events(socks, EVENT_READ, timeout) |
| 34 |
| 35 |
| 36 def wait_for_write(socks, timeout=None): |
| 37 """ Waits for writing to be available from a list of sockets |
| 38 or optionally a single socket if passed in. Returns a list of |
| 39 sockets that can be written to immediately. """ |
| 40 return _wait_for_io_events(socks, EVENT_WRITE, timeout) |
OLD | NEW |