* redesigned Asynchronizer.async_call (API Breakage)
This commit is contained in:
parent
39f6fcae7f
commit
e4a2ffc873
|
@ -1,3 +1,6 @@
|
||||||
|
0.2.15:
|
||||||
|
* API Breakage, changed the design of Asynchronizer.async_call.
|
||||||
|
|
||||||
0.2.14:
|
0.2.14:
|
||||||
* Changed the version numbering system.
|
* Changed the version numbering system.
|
||||||
* Fixed Authentication and MD5 with non-ASCII characters (issue #7)
|
* Fixed Authentication and MD5 with non-ASCII characters (issue #7)
|
||||||
|
|
74
pylast.py
74
pylast.py
|
@ -21,7 +21,7 @@
|
||||||
# http://code.google.com/p/pylast/
|
# http://code.google.com/p/pylast/
|
||||||
|
|
||||||
__name__ = 'pyLast'
|
__name__ = 'pyLast'
|
||||||
__version__ = '0.2.14'
|
__version__ = '0.2.15'
|
||||||
__doc__ = 'A Python interface to the Last.fm API.'
|
__doc__ = 'A Python interface to the Last.fm API.'
|
||||||
__author__ = 'Amr Hassan'
|
__author__ = 'Amr Hassan'
|
||||||
__email__ = 'amr.hassan@gmail.com'
|
__email__ = 'amr.hassan@gmail.com'
|
||||||
|
@ -139,61 +139,45 @@ class ServiceException(Exception):
|
||||||
|
|
||||||
return self._lastfm_status
|
return self._lastfm_status
|
||||||
|
|
||||||
class Asynchronizer(threading.Thread):
|
class _ThreadedFunct(threading.Thread):
|
||||||
"""Hopingly, this class would help perform asynchronous operations less painfully.
|
"""A class used by Asynchronizer."""
|
||||||
For inherited use only. And you must call Asynchronizer.__init__(descendant) before usage.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, sender, funct, funct_args, callback, callback_args):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
self._calls = {} #calls is structured like this: {call_pointer: (arg1, arg2, ...)}
|
self.funct = funct
|
||||||
self._callbacks = {} #callbacks is structred like this: {call_pointer: callback_pointer}
|
self.funct_args = funct_args
|
||||||
|
self.callback = callback
|
||||||
|
self.callback_args = callback_args
|
||||||
|
|
||||||
self.is_running = False
|
self.sender = sender
|
||||||
|
|
||||||
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):
|
def run(self):
|
||||||
"""Avoid running this function. Use start() to begin the thread's work."""
|
if self.funct:
|
||||||
|
if self.funct_args:
|
||||||
self.is_running = True
|
output = self.funct(*self.funct_args)
|
||||||
|
else:
|
||||||
while len(self._calls):
|
output = self.funct()
|
||||||
for call in self._calls.keys():
|
|
||||||
|
|
||||||
output = call(*(self._calls[call]))
|
if self.callback:
|
||||||
callback = self._callbacks[call]
|
if self.callback_args:
|
||||||
|
self.callback(self.sender, output, *self.callback_args)
|
||||||
if callback: #callback can be None if not wanted
|
else:
|
||||||
callback(self, output)
|
self.callback(self.sender, output)
|
||||||
|
|
||||||
del self._calls[call]
|
class Asynchronizer(object):
|
||||||
del self._callbacks[call]
|
"""This class helps performing asynchronous operations less painfully."""
|
||||||
|
|
||||||
self.is_running = False
|
|
||||||
|
|
||||||
def async_call(self, callback, call, *call_args):
|
def async_call(self, call, callback = None, call_args = None, callback_args = None):
|
||||||
"""This is the function for setting up an asynchronous operation.
|
"""This is the function for setting up an asynchronous operation.
|
||||||
* callback: the function to callback afterwards, accepting two argument, one being the sender and the other is the return of the target call.
|
* call: The function to call asynchronously.
|
||||||
* call: the target call.
|
* callback: The function to call after the operation is complete.
|
||||||
* *call_args: any number of arguments to pass to the target call function.
|
* call_args: A sequence of args to be passed to call.
|
||||||
|
* callback_args: A sequence of args to be passed to callback.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._calls[call] = call_args
|
thread = _ThreadedFunct(self, call, call_args, callback, callback_args)
|
||||||
self._callbacks[call] = callback
|
thread.start()
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
class Exceptionable(object):
|
class Exceptionable(object):
|
||||||
"""An abstract class that adds support for error reporting."""
|
"""An abstract class that adds support for error reporting."""
|
||||||
|
|
Loading…
Reference in a new issue