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' %callersprints 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 demonormally.
- Execute democoncurrently in a task.
- Execute demoin a thread.
In [1]:
Copied!
import ipywidgets as ipw
from async_kernel import AsyncEvent, 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 = AsyncEvent()
        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 ipywidgets as ipw
from async_kernel import AsyncEvent, 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 = AsyncEvent()
        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-13 (async_kernel_caller)
✓ Thread-15 (async_kernel_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-13 (async_kernel_caller)
✓ Thread-15 (async_kernel_caller)
Button(description='Continue', style=ButtonStyle())
Waiting 1
Running Protected Name
──────────────────────────────────────────────────────────────────────
✓ 🔐 MainThread ← current thread
✓ 🔐 ControlThread
✓ Thread-13 (async_kernel_caller)
✓ Thread-15 (async_kernel_caller)
Button(description='Continue', style=ButtonStyle())
Waiting 1