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

Commit 51cc765

Browse files
committed
add callback and pause to AsynchronousFunctions
1 parent ad739e6 commit 51cc765

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

fandango/threads.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,24 +1212,45 @@ class AsynchronousFunction(threading.Thread):
12121212
threading.Event().wait(0.1)
12131213
print 'result = ',result
12141214
'''
1215-
def __init__(self,function):
1216-
"""It just creates the function object, you must call function.start() afterwards"""
1215+
def __init__(self, function, args = None, kwargs = None,
1216+
callback=None, pause=0.0, start=False,
1217+
):
1218+
"""
1219+
It just creates the function object.
1220+
If pause!=0 or start=True, the function will be called
1221+
"""
12171222
self.function = function
12181223
self.result = None
12191224
self.exception = None
12201225
self.finished = threading.Event()
12211226
self.finished.clear()
12221227
threading.Thread.__init__(self)
1228+
self.callback = callback
1229+
self.pause = pause
12231230
self.wait = self.finished.wait
12241231
self.daemon = False
1232+
self.args = args or []
1233+
self.kwargs = kwargs or {}
1234+
if self.pause or start:
1235+
self.start()
1236+
12251237
def run(self):
12261238
try:
1227-
self.wait(0.01)
1228-
self.result = self.function()
1239+
if self.pause:
1240+
self.wait(self.pause)
1241+
self.result = self.function(*self.args, **self.kwargs)
12291242
except Exception,e:
12301243
self.result = None
12311244
self.exception = e
1232-
self.finished.set() #Not really needed, simply call AsynchronousFunction.isAlive() to know if it has finished
1245+
# Not really needed, simply call AsynchronousFunction.isAlive()
1246+
# to know if it has finished
1247+
self.finished.set()
1248+
if self.callback:
1249+
try:
1250+
self._bg = AsynchronousFunction(self.callback, start=True,
1251+
args = [self.result] if self.result is not None else [])
1252+
except:
1253+
traceback.print_exc()
12331254

12341255
from . import doc
12351256
__doc__ = doc.get_fn_autodoc(__name__,vars())

0 commit comments

Comments
 (0)