The parent side of workers (Effect's Worker).
A Worker[O, I] sends I messages to a worker and runs a handler for
each O it sends back. WorkerPlatform spawns workers by numeric id,
finding the platform's worker for each id with the Spawner function in
the context (layer_spawner). make_platform(setup=, listen=) builds a
platform from a transport's two hooks: setup starts the worker and
returns its port, listen hands what the worker sends to emit and
fails the run through deferred when the worker goes away. Messages sent
before the worker says it is ready are kept and sent once it is.
python_worker.layer is the platform for Python threads and child
processes (Effect's NodeWorker).
Re-exports#
Types#
| Type | Definition | Description |
|---|---|---|
PlatformMessage | type PlatformMessage = tuple[Literal[0]] | tuple[Literal[1], object] | What a worker sends its parent: |
Transfers | type Transfers = Sequence[object] | Values a platform may move rather than copy with a message (Effect's transfer list). The Python platforms copy or share every message and ignore them. |
SendFn | type SendFn = Callable[[Any, Transfers], Effect[None, WorkerError]] | |
RunFn | type RunFn = Callable[[Callable[[Any], Effect[Any, Any, Any]], Effect[None] | None], Effect[Never, Any, Any]] | |
SpawnerFn | type SpawnerFn = Callable[[int], object] | Gives the platform's worker for a worker id. |
Classes#
Functions#
make_unsafe#
make_unsafe(*, send, run) -> Worker[Any, Any]A Worker over a platform's raw send and run: run's
handler gets the PlatformMessages, and a ready message runs
on_spawn.
| Parameter | Type | Description |
|---|---|---|
send | SendFn | |
run | Callable[[Callable[[PlatformMessage], Effect[Any, Any, Any]]], Effect[Never, Any, Any]] |
Returns Worker[Any, Any]
def make_unsafe(
*,
send: SendFn,
run: Callable[[Callable[[PlatformMessage], Effect[Any, Any, Any]]], Effect[Never, Any, Any]],
) -> Worker[Any, Any]:
"""A ``Worker`` over a platform's raw ``send`` and ``run``: ``run``'s
handler gets the ``PlatformMessage``s, and a ready message runs
``on_spawn``."""
def run_worker(
handler: Callable[[Any], Effect[Any, Any, Any]], on_spawn: Effect[None] | None
) -> Effect[Never, Any, Any]:
spawned = _VOID if on_spawn is None else on_spawn
def on_message(message: PlatformMessage) -> Effect[Any, Any, Any]:
return spawned if message[0] == 0 else handler(message[1])
return run(on_message)
return Worker(send=send, run=run_worker)layer_spawner#
layer_spawner(spawner) -> Layer[Spawner]| Parameter | Type | Description |
|---|---|---|
spawner | SpawnerFn |
Returns Layer[Spawner]
def layer_spawner(spawner: SpawnerFn) -> Layer[Spawner]:
return layer_succeed(Spawner, spawner)make_platform#
make_platform[W, P: Port](*, setup, listen) -> WorkerPlatformServiceA WorkerPlatform from a transport's hooks (Effect's
makePlatform). setup starts the worker the Spawner gave and
returns its port, adding to scope whatever stops it (each run asks
the Spawner for a new worker); listen
wires the port's messages and failures.
| Parameter | Type | Description |
|---|---|---|
setup | Callable[[SetupOptions[W]], Effect[P, WorkerError]] | |
listen | Callable[[ListenOptions[P]], Effect[None]] |
Returns WorkerPlatformService
def make_platform[W, P: Port](
*,
setup: Callable[[SetupOptions[W]], Effect[P, WorkerError]],
listen: Callable[[ListenOptions[P]], Effect[None]],
) -> WorkerPlatformService:
"""A ``WorkerPlatform`` from a transport's hooks (Effect's
``makePlatform``). ``setup`` starts the worker the ``Spawner`` gave and
returns its port, adding to ``scope`` whatever stops it (each run asks
the ``Spawner`` for a new worker); ``listen``
wires the port's messages and failures."""
def spawn(worker_id: int) -> Effect[Worker[Any, Any], WorkerError, Spawner]:
def made(spawner: SpawnerFn) -> Worker[Any, Any]:
spawned = _Spawned[P](lambda: spawner(worker_id), cast("Any", setup), listen)
return Worker(send=spawned.send, run=spawned.run)
return Spawner().map(made)
return WorkerPlatformService(spawn)