]> git.phdru.name Git - bookmarks_db.git/commitdiff
Feat(bkmk_rtwisted): Use `threading.Event` for synchronization and timeouts
authorOleg Broytman <phd@phdru.name>
Sat, 8 Mar 2025 23:56:46 +0000 (02:56 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 8 Mar 2025 23:56:46 +0000 (02:56 +0300)
Robots/bkmk_rtwisted.py

index 0a4616fccc2eba80d5b362db492fdff73a950e14..67a95e0cb124c09565227c544d34ceec69004f93 100644 (file)
@@ -12,7 +12,6 @@ __all__ = ['robot_twisted']
 
 from urllib.parse import urlsplit
 import threading
-import time
 
 from twisted.internet import reactor, _sslverify
 from twisted.internet.endpoints import TCP4ClientEndpoint
@@ -32,6 +31,10 @@ reactor_thread.start()
 
 
 class robot_twisted(robot_base):
+    def __init__(self, *args, **kw):
+        robot_base.__init__(self, *args, **kw)
+        self.event = threading.Event()
+
     def version_str(self):
         return 'twisted/%s' % twisted.version
 
@@ -58,10 +61,8 @@ class robot_twisted(robot_base):
                           Headers(_headers), None)
         d.addCallbacks(self.cbResponse, self.cbError)
 
-        for i in range(self.timeout*10):
-            if self.error is None \
-                    and self.body is None:
-                time.sleep(0.1)
+        self.event.clear()
+        self.event.wait(self.timeout)
 
         if self.error is None:
             if self.body is None:
@@ -93,9 +94,11 @@ class robot_twisted(robot_base):
 
     def cbError(self, error):
         self.error = error
+        self.event.set()
 
     def cbBody(self, body):
         self.body = body
+        self.event.set()
 
 
 def decode_header(header):