Async ModuleΒΆ
Helper utils for Orange GUI programming.
Provides asynchronous()
decorator for making methods calls in async mode.
Once method is decorated it will have task.on_start()
, task.on_result()
and task.callback()
decorators for callbacks wrapping.
- on_start must take no arguments
- on_result must accept one argument (the result)
- callback can accept any arguments
For instance:
class Widget(QObject):
def __init__(self, name):
super().__init__()
self.name = name
@asynchronous
def task(self):
for i in range(3):
time.sleep(0.5)
self.report_progress(i)
return 'Done'
@task.on_start
def report_start(self):
print('`{}` started'.format(self.name))
@task.on_result
def report_result(self, result):
print('`{}` result: {}'.format(self.name, result))
@task.callback
def report_progress(self, i):
print('`{}` progress: {}'.format(self.name, i))
Calling an asynchronous method will launch a daemon thread:
first = Widget(name='First')
first.task()
second = Widget(name='Second')
second.task()
first.task.join()
second.task.join()
A possible output:
`First` started
`Second` started
`Second` progress: 0
`First` progress: 0
`First` progress: 1
`Second` progress: 1
`First` progress: 2
`First` result: Done
`Second` progress: 2
`Second` result: Done
In order to terminate a thread either call stop()
method or raise StopExecution
exception within task()
:
first.task.stop()