/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to avatarproviders.py

  • Committer: Curtis Hovey
  • Date: 2011-09-03 21:18:21 UTC
  • mto: This revision was merged to the branch mainline in revision 738.
  • Revision ID: sinzui.is@verizon.net-20110903211821-oitt1zqmzkanyhhg
Backport gtk3 test suite fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
        self.__queue = Queue.Queue()
56
56
 
57
57
        self.__provider_method = provider_method
58
 
        self.__end_thread = False
59
58
 
60
59
    def stop(self):
61
60
        """ Stop this worker """
62
 
        self.__end_thread = True
63
61
        self.__stop.set()
 
62
        while self.__queue.qsize() > 0:
 
63
            self.__queue.get_nowait()
 
64
            self.__queue.task_done()
 
65
        self.__queue.join()
 
66
 
 
67
    @property
 
68
    def is_running(self):
 
69
        return not self.__stop.is_set()
64
70
 
65
71
    def set_callback_method(self, method):
66
72
        """ Fire the given callback method when treatment is finished """
71
77
 
72
78
        This id_field is for example with Gravatar the email address.
73
79
        """
74
 
        self.__queue.put(id_field)
 
80
        if self.is_running:
 
81
            self.__queue.put(id_field)
 
82
            if not self.is_alive():
 
83
                self.start()
75
84
 
76
85
    def run(self):
77
86
        """Worker core code. """
78
 
        while not self.__end_thread:
79
 
            id_field = self.__queue.get()
80
 
            # Call provider method to get fields to pass in the request
81
 
            url = self.__provider_method(id_field)
82
 
            # Execute the request
83
 
            response = urllib2.urlopen(url)
84
 
            # Fire the callback method
85
 
            if not self.__callback_method is None:
86
 
                self.__callback_method(response, id_field)
87
 
            self.__queue.task_done()
 
87
        while self.is_running:
 
88
            try:
 
89
                id_field = self.__queue.get_nowait()
 
90
                # Call provider method to get fields to pass in the request
 
91
                url = self.__provider_method(id_field)
 
92
                # Execute the request
 
93
                response = urllib2.urlopen(url)
 
94
                # Fire the callback method
 
95
                if not self.__callback_method is None:
 
96
                    self.__callback_method(response, id_field)
 
97
                self.__queue.task_done()
 
98
            except Queue.Empty:
 
99
                # There is no more work to do.
 
100
                pass
88
101
 
89
102
 
90
103
class AvatarProviderGravatar(AvatarProvider):