pub struct Spawner { /* private fields */ }
Expand description
Handle to spawn tasks into an executor.
This Spawner can spawn any task (Send and non-Send ones), but it can only be used in the executor thread (it is not Send itself).
If you want to spawn tasks from another thread, use SendSpawner.
Implementations§
Source§impl Spawner
impl Spawner
Sourcepub unsafe fn for_current_executor() -> impl Future<Output = Self>
pub unsafe fn for_current_executor() -> impl Future<Output = Self>
Get a Spawner for the current executor.
This function is async
just to get access to the current async
context. It returns instantly, it does not block/yield.
Using this method is discouraged due to it being unsafe. Consider the following alternatives instead:
- Pass the initial
Spawner
as an argument to tasks. Note that it’sCopy
, so you can make as many copies of it as you want. - Use
SendSpawner::for_current_executor()
instead, which is safe but can only be used if task arguments areSend
.
The only case where using this method is absolutely required is obtaining the Spawner
for an InterruptExecutor
.
§Safety
You must only execute this with an async Context
created by the Embassy executor.
You must not execute it with manually-created Context
s.
§Panics
Panics if the current executor is not an Embassy executor.
Sourcepub fn spawn<S>(&self, token: SpawnToken<S>) -> Result<(), SpawnError>
pub fn spawn<S>(&self, token: SpawnToken<S>) -> Result<(), SpawnError>
Spawn a task into an executor.
You obtain the token
by calling a task function (i.e. one marked with #[embassy_executor::task]
).
Sourcepub fn must_spawn<S>(&self, token: SpawnToken<S>)
pub fn must_spawn<S>(&self, token: SpawnToken<S>)
Sourcepub fn make_send(&self) -> SendSpawner
pub fn make_send(&self) -> SendSpawner
Convert this Spawner to a SendSpawner. This allows you to send the spawner to other threads, but the spawner loses the ability to spawn non-Send tasks.
Sourcepub fn executor_id(&self) -> usize
pub fn executor_id(&self) -> usize
Return the unique ID of this Spawner’s Executor.
Trait Implementations§
Source§impl SpawnerTraceExt for Spawner
Implementation of the SpawnerTraceExt trait for Spawner when trace is disabled
impl SpawnerTraceExt for Spawner
Implementation of the SpawnerTraceExt trait for Spawner when trace is disabled