Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
add callback and pause to AsynchronousFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
sergirubio committed Feb 13, 2020
1 parent ad739e6 commit 51cc765
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions fandango/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,24 +1212,45 @@ class AsynchronousFunction(threading.Thread):
threading.Event().wait(0.1)
print 'result = ',result
'''
def __init__(self,function):
"""It just creates the function object, you must call function.start() afterwards"""
def __init__(self, function, args = None, kwargs = None,
callback=None, pause=0.0, start=False,
):
"""
It just creates the function object.
If pause!=0 or start=True, the function will be called
"""
self.function = function
self.result = None
self.exception = None
self.finished = threading.Event()
self.finished.clear()
threading.Thread.__init__(self)
self.callback = callback
self.pause = pause
self.wait = self.finished.wait
self.daemon = False
self.args = args or []
self.kwargs = kwargs or {}
if self.pause or start:
self.start()

def run(self):
try:
self.wait(0.01)
self.result = self.function()
if self.pause:
self.wait(self.pause)
self.result = self.function(*self.args, **self.kwargs)
except Exception,e:
self.result = None
self.exception = e
self.finished.set() #Not really needed, simply call AsynchronousFunction.isAlive() to know if it has finished
# Not really needed, simply call AsynchronousFunction.isAlive()
# to know if it has finished
self.finished.set()
if self.callback:
try:
self._bg = AsynchronousFunction(self.callback, start=True,
args = [self.result] if self.result is not None else [])
except:
traceback.print_exc()

from . import doc
__doc__ = doc.get_fn_autodoc(__name__,vars())
Expand Down

0 comments on commit 51cc765

Please sign in to comment.