OSDN Git Service

bug fix: add lock into twitter api wrapper (python-twoauth is not threadsafe?)
authorHirotaka Kawata <info@techno-st.net>
Thu, 30 Sep 2010 12:25:06 +0000 (21:25 +0900)
committerHirotaka Kawata <info@techno-st.net>
Thu, 30 Sep 2010 12:25:06 +0000 (21:25 +0900)
gwit/timelinethread.py
gwit/twitterapi.py

index 3967df6..da6ef74 100644 (file)
@@ -73,7 +73,6 @@ class TimelineThread(BaseThread):
         while not self.die:
             apimethod = getattr(self.twitter.api, self.method)
             statuses = self.twitter.api_wrapper(apimethod, *self.args, **self.kwargs)
-            
             # If Timeline update
             if statuses:
                 # Add statuses to timeline
@@ -82,7 +81,6 @@ class TimelineThread(BaseThread):
                 # update lastid
                 self.lastid = statuses[-1].id
                 self.kwargs["since_id"] = self.lastid
-            
             # Reload lock
             self.lock.clear()
             if self.interval != -1:
index eea8991..ac54ef6 100644 (file)
@@ -45,6 +45,7 @@ class TwitterAPI:
         self.me = None
         self.my_name = screen_name
         #self.threads = list()
+        self.apilock = threading.Lock()
         
         # User, Status Buffer
         self.users = dict()
@@ -95,6 +96,7 @@ class TwitterAPI:
     def api_wrapper(self, method, *args, **kwargs):
         for i in range(3):
             try:
+                self.apilock.acquire()
                 response = None
                 response = method(*args, **kwargs)
                 break
@@ -110,13 +112,17 @@ class TwitterAPI:
                     break
                 
                 if i >= 3:
-                    self.on_twitterapi_error(self, e)
+                    self.on_twitterapi_error(method, e)
+                
                 print >>sys.stderr, "[Error] %d: TwitterAPI %s (%s)" % (i, e, method.func_name)
-                time.sleep(5)
             except socket.timeout:
                 print >>sys.stderr, "[Error] %d: TwitterAPI timeout (%s)" % (i, method.func_name)
             except Exception, e:
                 print >>sys.stderr, "[Error] %d: TwitterAPI %s (%s)" % (i, e, method.func_name)
+            finally:
+                self.apilock.release()
+            
+            time.sleep(5)
         
         return response