[tor-commits] [stem/master] Make `_get_with_timeout` asynchronous

atagar at torproject.org atagar at torproject.org
Thu Jul 16 01:28:58 UTC 2020


commit e43c446d991611c421dbae68f7c46fb922589e0d
Author: Illia Volochii <illia.volochii at gmail.com>
Date:   Fri Apr 17 23:06:52 2020 +0300

    Make `_get_with_timeout` asynchronous
---
 stem/control.py | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 1a107a79..84efbf81 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -3981,20 +3981,12 @@ def _case_insensitive_lookup(entries: Union[Sequence[str], Mapping[str, Any]], k
   raise ValueError("key '%s' doesn't exist in dict: %s" % (key, entries))
 
 
-def _get_with_timeout(event_queue: queue.Queue, timeout: float, start_time: float) -> Any:
+async def _get_with_timeout(event_queue: queue.Queue, timeout: float, start_time: float) -> Any:
   """
   Pulls an item from a queue with a given timeout.
   """
 
-  if timeout:
-    time_left = timeout - (time.time() - start_time)
-
-    if time_left <= 0:
-      raise stem.Timeout('Reached our %0.1f second timeout' % timeout)
-
-    try:
-      return event_queue.get(True, time_left)
-    except asyncio.queues.QueueEmpty:
-      raise stem.Timeout('Reached our %0.1f second timeout' % timeout)
-  else:
-    return event_queue.get()
+  try:
+    return await asyncio.wait_for(event_queue.get(), timeout=timeout)
+  except asyncio.TimeoutError:
+    raise stem.Timeout('Reached our %0.1f second timeout' % timeout)





More information about the tor-commits mailing list