Function mesh_worker::worker_host
source · pub fn worker_host() -> (WorkerHost, WorkerHostRunner)
Expand description
Returns a new WorkerHost
, WorkerHostRunner
pair.
The WorkerHost
is used to launch workers, while the WorkerHostRunner
is used to handle worker launch requests. The caller must start
WorkerHostRunner::run()
on an appropriate task before WorkerHost
will
be able to launch workers.
This is useful over just using launch_local_worker
because it provides
an indirection between the identity of the workers being launched
(identified via WorkerId
) and the concrete worker implementation. This
can be used to swap worker implementations, improve build times, and to
support launching workers across process boundaries.
To achieve this latter feat, note that either half of the returned tuple may be sent to over a mesh channel to another process, allowing a worker to be spawned in a separate process from the caller. This can be useful for fault or resource isolation and for security sandboxing.
§Example
let (host, runner) = worker_host();
// Run the worker host on a separate thread. (Typically this would just be
// a separate task in your async framework.)
std::thread::spawn(|| block_on(runner.run(RegisteredWorkers)));
// Launch a worker by ID. This will call to the worker host runner.
host.launch_worker(MY_WORKER, ()).await.unwrap();