summaryrefslogtreecommitdiffstats
path: root/python.d/python_modules/urllib3/util/wait.py
diff options
context:
space:
mode:
authorLennart Weller <lhw@ring0.de>2017-09-17 22:17:45 +0000
committerLennart Weller <lhw@ring0.de>2017-09-17 22:17:45 +0000
commit7ee3962eaca4214264964ae32c86de457a90e382 (patch)
tree5a784485351593ac6d74fa9fd9bbd8fe7c9c3fe0 /python.d/python_modules/urllib3/util/wait.py
parentmaintscript eludes me (diff)
parentNew upstream version 1.8.0+dfsg (diff)
downloadnetdata-7ee3962eaca4214264964ae32c86de457a90e382.tar.xz
netdata-7ee3962eaca4214264964ae32c86de457a90e382.zip
Updated version 1.8.0+dfsg from 'upstream/1.8.0+dfsg'
with Debian dir 412de09d9bca38fe00146ef090f9e53f76493882
Diffstat (limited to 'python.d/python_modules/urllib3/util/wait.py')
-rw-r--r--python.d/python_modules/urllib3/util/wait.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/python.d/python_modules/urllib3/util/wait.py b/python.d/python_modules/urllib3/util/wait.py
new file mode 100644
index 000000000..cb396e508
--- /dev/null
+++ b/python.d/python_modules/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)