import tulip import viol from demolib import set_title, make_widget set_title('Foo and bar run at the same time') @viol.connect('#go click') def start(client, data): task1 = tulip.Task(foo(client)) task2 = tulip.Task(bar(client)) while True: if task1.done() and task2.done(): client.log(task1.result()) client.log(task2.result()) break else: yield from tulip.sleep(1) def foo(client): for i in range(10): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): for i in range(10): widget = yield from make_widget()
import tulip import viol from demolib import set_title, make_widget set_title('Do something after foo and bar are both done') @viol.connect('#go click') def start(client, data): coroutines = [foo(client), bar(client)] done, pending = yield from tulip.wait(coroutines) for future in done: client.log(future.result()) def foo(client): for i in range(10): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): for i in range(10): widget = yield from make_widget() client.append_widget('bar', widget) return 'bar is done!' if __name__ == '__main__': viol.run()
import tulip import viol from demolib import set_title, make_widget, make_widget_synchronous set_title('Bar runs code in another thread') @viol.connect('#go click') def start(client, data): coroutines = [foo(client), bar(client)] for future in tulip.as_completed(coroutines): result = yield from future client.log(result) def foo(client): client.log('Starting foo.') for i in range(8): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): client.log('Starting bar work in another thread.') loop = tulip.get_event_loop() widgets = yield from loop.run_in_executor(None, bar_synchronous) for widget in widgets: client.append_widget('bar', widget) return 'bar is done!' def bar_synchronous(): widgets = [] for i in range(12):
import tulip import viol from demolib import set_title, make_widget set_title('Do something when either foo or bar finishes') @viol.connect('#go click') def start(client, data): coroutines = [foo(client), bar(client)] for future in tulip.as_completed(coroutines): result = yield from future client.log(result) def foo(client): for i in range(12): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): for i in range(8): widget = yield from make_widget() client.append_widget('bar', widget) return 'bar is done!' if __name__ == '__main__': viol.run()
import tulip import viol from demolib import set_title, make_widget, make_widget_synchronous set_title('Bar runs in another thread but communicates with event loop') @viol.connect('#go click') def start(client, data): coroutines = [foo(client), bar(client)] for future in tulip.as_completed(coroutines): result = yield from future client.log(result) def foo(client): client.log('Starting foo.') for i in range(10): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): client.log('Starting bar work in another thread.') loop = tulip.get_event_loop() yield from loop.run_in_executor(None, bar_synchronous, loop, client) return 'bar is done!' def bar_synchronous(loop, client):
import tulip import viol from demolib import set_title, make_widget, make_widget_synchronous set_title('Bar runs in another thread but communicates with event loop') @viol.connect('#go click') def start(client, data): coroutines = [foo(client), bar(client)] for future in tulip.as_completed(coroutines): result = yield from future client.log(result) def foo(client): client.log('Starting foo.') for i in range(10): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): client.log('Starting bar work in another thread.') loop = tulip.get_event_loop() yield from loop.run_in_executor(None, bar_synchronous, loop, client) return 'bar is done!' def bar_synchronous(loop, client): for i in range(10): widget = make_widget_synchronous() loop.call_soon_threadsafe(client.append_widget, 'bar', widget) loop.call_soon_threadsafe(client.log,
import viol from demolib import set_title, make_widget set_title('Foo runs, then bar runs') @viol.connect('#go click') def start(client, data): result = yield from foo(client) client.log(result) result = yield from bar(client) client.log(result) def foo(client): for i in range(10): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): for i in range(10): widget = yield from make_widget() client.append_widget('bar', widget) return 'bar is done!' if __name__ == '__main__': viol.run()
import tulip import viol from demolib import set_title, make_widget set_title("Do something after foo and bar are both done") @viol.connect("#go click") def start(client, data): coroutines = [foo(client), bar(client)] done, pending = yield from tulip.wait(coroutines) for future in done: client.log(future.result()) def foo(client): for i in range(10): widget = yield from make_widget() client.append_widget("foo", widget) return "foo is done!" def bar(client): for i in range(10): widget = yield from make_widget() client.append_widget("bar", widget) return "bar is done!" if __name__ == "__main__": viol.run()
import tulip import viol from demolib import set_title, make_widget set_title('Foo and bar run at the same time') @viol.connect('#go click') def start(client, data): task1 = tulip.Task(foo(client)) task2 = tulip.Task(bar(client)) while True: if task1.done() and task2.done(): client.log(task1.result()) client.log(task2.result()) break else: yield from tulip.sleep(1) def foo(client): for i in range(10): widget = yield from make_widget() client.append_widget('foo', widget) return 'foo is done!' def bar(client): for i in range(10): widget = yield from make_widget() client.append_widget('bar', widget) return 'bar is done!'