Added locking and moved work around / reduced repeat work at startup. Should clear up segfaults and improve startup time

This commit is contained in:
Anna
2010-05-04 11:03:32 -04:00
parent 7cce83c0f3
commit 6044fa27d2
3 changed files with 61 additions and 39 deletions

View File

@ -2,7 +2,19 @@
import re
import gtk
from threading import Thread
from threading import Thread,RLock
from twitter import Api
class SafeApi(Api):
''' This is just a Twitter API with an RLock for multi-threaded access '''
def __init__(self, username, password):
Api.__init__(self, username, password)
self.lock = RLock()
# End class SafeApi
class GetTweets(Thread):
def __init__(self, api, list_name, pane, num_entries, username):
@ -15,33 +27,35 @@ class GetTweets(Thread):
def run(self):
# username/Home entries need to load the appropriate Home feed
if self.list_name == self.username + '/Home':
statuses = self.api.GetHomeTimeline(count=self.num_entries)
with self.api.lock:
# For @username, check if it is one of our usernames, or
# just needs to be searched on
elif self.list_name == '@' + self.username:
statuses = self.api.GetMentions(count=self.num_entries)
elif re.match('@', self.list_name):
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
# username/Home entries need to load the appropriate Home feed
if self.list_name == self.username + '/Home':
statuses = self.api.GetHomeTimeline(count=self.num_entries)
# Direct Messages should match like /Home, above
elif self.list_name == self.username + '/Direct Messages':
statuses = dms_to_statuses(self.api.GetDirectMessages())
# For @username, check if it is one of our usernames, or
# just needs to be searched on
elif self.list_name == '@' + self.username:
statuses = self.api.GetMentions(count=self.num_entries)
elif re.match('@', self.list_name):
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
# User lookups go straight to the user
elif re.match(r'user: ', self.list_name):
statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', self.list_name), count=self.num_entries)
# Direct Messages should match like /Home, above
elif self.list_name == self.username + '/Direct Messages':
statuses = dms_to_statuses(self.api.GetDirectMessages())
# Lists load the appropriate list from the appropriate account
elif re.match(r'list: ', self.list_name):
real_list = re.sub(r'list: .*/(.*)', r'\1', self.list_name)
statuses = self.api.GetListStatuses(real_list, per_page=self.num_entries)
# User lookups go straight to the user
elif re.match(r'user: ', self.list_name):
statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', self.list_name), count=self.num_entries)
# Everything else is a straight search
else:
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
# Lists load the appropriate list from the appropriate account
elif re.match(r'list: ', self.list_name):
real_list = re.sub(r'list: .*/(.*)', r'\1', self.list_name)
statuses = self.api.GetListStatuses(real_list, per_page=self.num_entries)
# Everything else is a straight search
else:
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
gtk.gdk.threads_enter()
try:
@ -64,7 +78,8 @@ class GetSingleTweet(Thread):
def run(self):
statuses = []
statuses.append(self.api.GetStatus(self.single_tweet))
with self.api.lock:
statuses.append(self.api.GetStatus(self.single_tweet))
gtk.gdk.threads_enter()
try:
@ -89,7 +104,8 @@ class GetFollowing(Thread):
screen_name = re.sub('user: ', '', self.user)
try:
relationship = self.api.ShowFriendships(target_screen_name=screen_name)
with self.api.lock:
relationship = self.api.ShowFriendships(target_screen_name=screen_name)
following = relationship.source.following
except HTTPError:
following = false
@ -112,7 +128,8 @@ class GetVerified(Thread):
screen_name = re.sub('user: ', '', self.user)
try:
user = self.api.GetUser(screen_name)
with self.api.lock:
user = self.api.GetUser(screen_name)
verified = user.verified
except HTTPError:
verified = false