Synchronisation¤
tinyio.as_completed(coros: set[Coro[~_T]]) -> Coro[Iterable[Coro[~_T]]]
¤
Schedules multiple coroutines, iterating through their outputs in the order that they complete.
Usage is as follows:
import tinyio
def sleep(x):
yield tinyio.sleep(x)
return x
def as_completed_demo():
for out in (yield tinyio.as_completed({sleep(7), sleep(2), sleep(4)})):
out = yield out
print(f"As completed demo: {out}")
loop = tinyio.Loop()
loop.run(as_completed_demo())
# As completed demo: 2
# As completed demo: 4
# As completed demo: 7
tinyio.Barrier
¤
Prevents coroutines from progressing until at least value of them have called yield barrier.wait().
Usage:
barrier = tinyio.Barrier(value=2)
def coro1():
yield barrier
...
def coro2():
yield barrier
...
... will execute until both coroutines have reached the barrier.
tinyio.Event
¤
A marker that something has happened.
is_set() -> bool
¤
Arguments: None.
Returns:
Whether the event has been set.
set()
¤
Sets the event. Any coroutines blocked on yield event.wait() will be woken up, and .is_set() will now
return True.
Arguments: None.
Returns: None.
clear()
¤
Clears the event. Any coroutines that later call yield event.wait() will be blocked until the event is set,
and .is_set() will now return False.
Arguments: None.
Returns: None.
wait(timeout_in_seconds: None | int | float = None) -> Coro[None]
¤
Waits until the event is set.
Usage:
import tinyio
event = tinyio.Event()
def foo():
yield event.wait()
print("running foo")
def bar():
print("running bar")
yield
event.set()
def run():
yield [foo(), bar()]
tinyio.Loop().run(run())
# running bar
# running foo
foo is not able to run until bar has set the event.
Arguments:
timeout_in_seconds: the maximum time to wait. After this long then the coroutine waiting on this event will unblock anyway.
Returns:
A coroutine that can be yielded.
tinyio.Lock
¤
Prevents multiple coroutines from accessing a single resource.
Usage:
lock = tinyio.Lock()
def coro1():
with (yield lock()):
...
def coro2():
with (yield lock()):
...
coro1 or coro2 will be able to run inside their ... region at time.
Note that this class is just a convenience wrapper for tinyio.Semaphore(value=1).
tinyio.Semaphore
¤
Limits coroutines so that at most value of them can access a resource concurrently.
Usage:
semaphore = tinyio.Semaphore(value=...)
with (yield semaphore()):
...