Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
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):
Exemple #4
0
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()
Exemple #5
0
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):
Exemple #6
0
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,
Exemple #7
0
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()
Exemple #8
0
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()
Exemple #9
0
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!'
Exemple #10
0
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()