Caller¶
Caller is a class that makes it easy to call code in different threads/tasks.
One caller instance is created per thread and can start children callers in new threads by name or using a pool of workers with the 'to_thread' method.
Pending¶
If a method call returns an async_kernel.Pending the result of the call can be obtained by awaiting the pending.
Usage by the kernel¶
The kernel uses two Caller instances; one each for shell and control. The shell thread event loop is normally the MainThread,
but could be any thread depending on where the kernel was started. The control event loop is always named ControlThread and is a child of the shell caller. The
shell caller is accessible at kernel.caller while the kerenel is running.
In [1]:
Copied!
# A magic method provided by async kernel
%callers
# A magic method provided by async kernel
%callers
Name Running Protected Thread
────────────────────────────────────────────────────────
Shell ✓ 🔐 140657493307520 ← current
Control ✓ 🔐 140657418278592
In [2]:
Copied!
from async_kernel import Caller
Caller()
from async_kernel import Caller
Caller()
Caller<Shell at 140657441055152 asyncio 🏃 running 1 child>
Example¶
This example requires ipywidgets!
In [3]:
Copied!
import random
import time
import ipywidgets as ipw
outputs = {}
stop = ipw.RadioButtons(value=None, options=["Stop"])
display(stop)
def my_func(n):
caller = Caller()
if not (out := outputs.get(caller)):
outputs[caller] = out = ipw.HTML(description=str(caller), style={"description_width": "initial"})
display(out)
sleep_time = random.random() / 4
out.value = f"{n=:04d} sleeping {sleep_time * 1000:03.0f} ms"
time.sleep(sleep_time)
return n
async def run_forever():
n = 0
while not stop.value:
n += 1
yield Caller().to_thread(my_func, n)
async for fut in Caller().as_completed(run_forever()):
result = await fut
print(f"Finished: {result}", end="\r")
import random
import time
import ipywidgets as ipw
outputs = {}
stop = ipw.RadioButtons(value=None, options=["Stop"])
display(stop)
def my_func(n):
caller = Caller()
if not (out := outputs.get(caller)):
outputs[caller] = out = ipw.HTML(description=str(caller), style={"description_width": "initial"})
display(out)
sleep_time = random.random() / 4
out.value = f"{n=:04d} sleeping {sleep_time * 1000:03.0f} ms"
time.sleep(sleep_time)
return n
async def run_forever():
n = 0
while not stop.value:
n += 1
yield Caller().to_thread(my_func, n)
async for fut in Caller().as_completed(run_forever()):
result = await fut
print(f"Finished: {result}", end="\r")
RadioButtons(options=('Stop',), value=None)
HTML(value='', description='Caller<async_kernel_caller at 140657420723328 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420730784 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420728272 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420732320 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420736544 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420673504 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420670816 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420669712 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420736880 asyncio 🏃 running >', style=HTMLStyl…
HTML(value='', description='Caller<async_kernel_caller at 140657420745376 asyncio 🏃 running >', style=HTMLStyl…
In [4]:
Copied!
%callers
%callers
Name Running Protected Thread
────────────────────────────────────────────────────────────────────
Shell ✓ 🔐 140657493307520 ← current
Control ✓ 🔐 140657418278592
async_kernel_caller ✓ 140656924153536
async_kernel_caller ✓ 140656622171840
async_kernel_caller ✓ 140656605390528
async_kernel_caller ✓ 140656588609216
async_kernel_caller ✓ 140656353736384
async_kernel_caller ✓ 140656336955072
async_kernel_caller ✓ 140656320173760
async_kernel_caller ✓ 140656085300928
async_kernel_caller ✓ 140656068519616
async_kernel_caller ✓ 140656051738304