diff options
| author | Yuval Adam <_@yuv.al> | 2017-11-17 14:54:19 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2017-11-17 14:54:19 +0200 |
| commit | 1c703284cfa73350b794d50795133f2c2ea26f0b (patch) | |
| tree | 73b2fb83395ac2ed27a47ce5d1a6f080b536b305 /cron/urllib3/util/wait.py | |
| parent | f6b1c6ad35accad8d93370024d7f0b3088f32bdc (diff) | |
Add lambda cron task
Diffstat (limited to 'cron/urllib3/util/wait.py')
| -rw-r--r-- | cron/urllib3/util/wait.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/cron/urllib3/util/wait.py b/cron/urllib3/util/wait.py new file mode 100644 index 0000000..cb396e5 --- /dev/null +++ b/cron/urllib3/util/wait.py @@ -0,0 +1,40 @@ +from .selectors import ( + HAS_SELECT, + DefaultSelector, + EVENT_READ, + EVENT_WRITE +) + + +def _wait_for_io_events(socks, events, timeout=None): + """ Waits for IO events to be available from a list of sockets + or optionally a single socket if passed in. Returns a list of + sockets that can be interacted with immediately. """ + if not HAS_SELECT: + raise ValueError('Platform does not have a selector') + if not isinstance(socks, list): + # Probably just a single socket. + if hasattr(socks, "fileno"): + socks = [socks] + # Otherwise it might be a non-list iterable. + else: + socks = list(socks) + with DefaultSelector() as selector: + for sock in socks: + selector.register(sock, events) + return [key[0].fileobj for key in + selector.select(timeout) if key[1] & events] + + +def wait_for_read(socks, timeout=None): + """ Waits for reading to be available from a list of sockets + or optionally a single socket if passed in. Returns a list of + sockets that can be read from immediately. """ + return _wait_for_io_events(socks, EVENT_READ, timeout) + + +def wait_for_write(socks, timeout=None): + """ Waits for writing to be available from a list of sockets + or optionally a single socket if passed in. Returns a list of + sockets that can be written to immediately. """ + return _wait_for_io_events(socks, EVENT_WRITE, timeout) |
