Skip to main content

spawn

Function spawn 

Source
pub fn spawn<S, T>(child: T) -> ChildId
Expand description

Spawns a child of the current process.

The scope of the current process holds the child. The process does not finish until the child finishes, and the child is told to stop when the body of the process returns.

Some code does not run as a supervised process, but has an ambient supervisor from SupervisorHandle::scope. An example is a test that drives a component directly. That code spawns onto the ambient supervisor instead.

Accepts anything Supervisor::add_worker accepts: a bare Supervisable, a Supervisor to run as a nested supervision subtree, or a ChildSpecification configured in detail. The child is hosted, unless the specification says otherwise. See ChildBuilder for what that means.

This function has the same shape and guarantees as tokio::spawn. It always succeeds, but success means only that the child was accepted. It does not guarantee that the child runs. If an owner stops before it gets to the queued child, it never starts that child.

§Panics

Panics if there is no current scope and no ambient supervisor. This means that the caller does not run as a supervised process, or in a supervised process. Establish an ambient supervisor with SupervisorHandle::scope, or spawn through a SupervisorHandle directly.

§Examples

spawn(FnWorker::new("refresher", refresh()));