Simple example
Overview¶
This example demonstrates different ways that the same code can be executed. This is an overview of the four cells shown in the animation below.
- Define the coroutine function.
- Cell 'magic'
%callers
prints a list of Caller instances and the thread in which it is executing. - A button is created and and it runs a loop twice:
- Creates any anyio event.
- Prints a statement.
- Waits for the button click to set the event.
- Cell 'magic'
- Execute
demo
normally. - Execute
demo
concurrently in a task. - Execute
demo
in a thread.
In [1]:
Copied!
import anyio
import ipywidgets as ipw
from async_kernel import Caller, utils
async def demo():
%callers
caller = Caller() # Use caller set the event in the waiting thread
b = ipw.Button(description="Continue")
display(b)
for i in range(1, 3):
b.description = f"Continue {i}"
event = anyio.Event()
b.on_click(lambda _: caller.call_soon(event.set)) # noqa: B023
print(f"Waiting {i}", end="\r")
await event.wait()
b.close()
print("\nDone!")
import anyio
import ipywidgets as ipw
from async_kernel import Caller, utils
async def demo():
%callers
caller = Caller() # Use caller set the event in the waiting thread
b = ipw.Button(description="Continue")
display(b)
for i in range(1, 3):
b.description = f"Continue {i}"
event = anyio.Event()
b.on_click(lambda _: caller.call_soon(event.set)) # noqa: B023
print(f"Waiting {i}", end="\r")
await event.wait()
b.close()
print("\nDone!")
In [2]:
Copied!
print(utils.get_tags())
await demo()
print(utils.get_tags())
await demo()
[]
Running Protected Name
──────────────────────────────────────────────────────────────────────
✓ 🔐 MainThread ← current thread
✓ 🔐 ControlThread
Button(description='Continue', style=ButtonStyle())
Waiting 1
In [3]:
Copied!
##task
await demo()
##task
await demo()
Running Protected Name
──────────────────────────────────────────────────────────────────────
✓ 🔐 MainThread ← current thread
✓ 🔐 ControlThread
Button(description='Continue', style=ButtonStyle())
Waiting 1
In [4]:
Copied!
##thread
await demo()
##thread
await demo()
Running Protected Name
──────────────────────────────────────────────────────────────────────
✓ 🔐 MainThread
✓ 🔐 ControlThread
✓ Thread-3 (anyio_run_caller)
✓ Thread-4 (anyio_run_caller) ← current thread
Button(description='Continue', style=ButtonStyle())
Waiting 1
In [5]:
Copied!
async for _ in Caller.as_completed(Caller().call_soon(demo) for _ in range(2)):
pass
async for _ in Caller.as_completed(Caller().call_soon(demo) for _ in range(2)):
pass
Running Protected Name
──────────────────────────────────────────────────────────────────────
✓ 🔐 MainThread ← current thread
✓ 🔐 ControlThread
✓ Thread-3 (anyio_run_caller)
✓ Thread-4 (anyio_run_caller)
Button(description='Continue', style=ButtonStyle())
Waiting 1
Running Protected Name
──────────────────────────────────────────────────────────────────────
✓ 🔐 MainThread ← current thread
✓ 🔐 ControlThread
✓ Thread-3 (anyio_run_caller)
✓ Thread-4 (anyio_run_caller)
Button(description='Continue', style=ButtonStyle())
Waiting 1