From 7e203ee5a24511dbad450f1bec2c7afe9a9c74b5 Mon Sep 17 00:00:00 2001 From: Amr Hassan Date: Fri, 12 Sep 2008 15:49:13 +0000 Subject: [PATCH] * fixed: crashes when adding a job to Asynchronizer and starting it when it already started. --- changes.txt | 3 +++ pylast.py | 37 ++++++++++++++++++++++++++----------- setup.py | 2 +- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/changes.txt b/changes.txt index abd5b72..0ec2d0e 100644 --- a/changes.txt +++ b/changes.txt @@ -1,3 +1,6 @@ +0.2b9 + * fixed: crashes when adding a job to Asynchronizer and starting it when it already started. + 0.2b8 * Asynchronizer.async_call now accepts None as callback. * moved all the tag related functions to a separate Taggable class. diff --git a/pylast.py b/pylast.py index 1c76b56..283f1aa 100644 --- a/pylast.py +++ b/pylast.py @@ -22,7 +22,7 @@ # documentation at http://code.google.com/p/pylast/wiki/Documentation LIB_NAME = 'pyLast' -LIB_VERSION = '0.2b8' +LIB_VERSION = '0.2b9' API_SERVER = 'ws.audioscrobbler.com' API_SUBDIR = '/2.0/' @@ -122,20 +122,32 @@ class Asynchronizer(threading.Thread): self._calls = {} #calls is structured like this: {call_pointer: (arg1, arg2, ...)} self._callbacks = {} #callbacks is structred like this: {call_pointer: callback_pointer} + + self.is_running = False + + def isRunning(self): + """Returns if the thread is already running, for usage instead of isAlive which doesn't believe in thread restarting.""" + + return self.is_running def run(self): """Avoid running this function. Use start() to begin the thread's work.""" - for call in self._calls.keys(): - - output = call(*(self._calls[call])) - callback = self._callbacks[call] - - if callback: #callback can be None if not wanted - callback(self, output) - - del self._calls[call] - del self._callbacks[call] + self.is_running = True + + while len(self._calls): + for call in self._calls.keys(): + + output = call(*(self._calls[call])) + callback = self._callbacks[call] + + if callback: #callback can be None if not wanted + callback(self, output) + + del self._calls[call] + del self._callbacks[call] + + self.is_running = False def async_call(self, callback, call, *call_args): """This is the function for setting up an asynchronous operation. @@ -150,6 +162,9 @@ class Asynchronizer(threading.Thread): def start(self): """Since that Python thread objects can only be started once. This is my little work-around.""" + if self.isRunning(): + return + threading.Thread.__init__(self) super(Asynchronizer, self).start() diff --git a/setup.py b/setup.py index de89cdc..38727b0 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from distutils.core import setup setup(name='pylast', - version='0.2b8', + version='0.2b9', author='Amr Hassan', long_description = 'Python bindings for the Last.fm API 2.0', author_email='amr.hassan@gmail.com',