pub struct BackgroundTasks { /* private fields */ }Expand description
The worker pool itself. Insert as a resource once, at startup; every
system that needs to offload work reaches for Res<BackgroundTasks>
and calls spawn.
Cheap to clone (an internal channel sender) — clone it out of a Res
borrow to move an owned handle into a detached future, e.g. from a
system registered with AsyncExt::detach.
Implementations§
Source§impl BackgroundTasks
impl BackgroundTasks
Sourcepub fn new(worker_count: usize) -> Self
pub fn new(worker_count: usize) -> Self
Spawns worker_count OS threads, each pulling jobs off one shared,
lock-free queue until the pool itself is dropped. A worker count
around your CPU’s core count (minus one, to leave room for the
main thread) is a reasonable default; tune based on actual
measured load.
On wasm32 there are no OS threads to spawn, so worker_count is
ignored and spawn_blocking queues jobs that never run —
spawn_async is the one that’s web-compatible,
since it drives the browser’s microtask queue via
wasm_bindgen_futures::spawn_local instead of a worker thread.
Sourcepub fn spawn_blocking<T: Send + 'static>(
&self,
work: impl FnOnce() -> T + Send + 'static,
) -> TaskHandle<T>
pub fn spawn_blocking<T: Send + 'static>( &self, work: impl FnOnce() -> T + Send + 'static, ) -> TaskHandle<T>
Queue work to run on the pool. Returns a TaskHandle you can
poll (non-blocking) from any system to check whether it’s done.
work runs on whichever worker thread picks it up next — don’t
assume anything about timing or ordering relative to other spawned
tasks unless you build that coordination yourself.
Native-only. There are no OS threads to block on in a browser
tab, so on wasm32 this queues a job that never runs — use
spawn_async instead, which works on both.
The _blocking suffix names what makes this one platform-specific:
it occupies its worker thread for as long as work runs, same as
std::thread::spawn would.
Sourcepub fn spawn_async<T: Send + 'static>(
&self,
future: impl SpawnableFuture<T>,
) -> TaskHandle<T>
pub fn spawn_async<T: Send + 'static>( &self, future: impl SpawnableFuture<T>, ) -> TaskHandle<T>
Queue an already-constructed future to run to completion, and
return a TaskHandle you can poll for its result.
This is the primitive AsyncExt::detach
uses under the hood; call it directly instead when you want the
TaskHandle back to poll for a result, rather than firing the
future off and forgetting it.
- Native: blocks whichever worker thread picks it up (via
pollster::block_on) for as long as the future takes to resolve — there’s no cooperative multitasking between futures sharing a worker, so a slow future occupies that worker exclusively, same as a slowspawn_blockingclosure would. The future must beSendto cross to that worker thread. - Web: runs on the browser’s microtask queue via
wasm_bindgen_futures::spawn_local, on the same (only) thread — so it does not need to beSend, which is what lets it capture!Sendbrowser-bound types (JsValue, aweb_syshandle, …).
Trait Implementations§
Source§impl Clone for BackgroundTasks
impl Clone for BackgroundTasks
Source§fn clone(&self) -> BackgroundTasks
fn clone(&self) -> BackgroundTasks
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for BackgroundTasks
impl RefUnwindSafe for BackgroundTasks
impl Send for BackgroundTasks
impl Sync for BackgroundTasks
impl Unpin for BackgroundTasks
impl UnsafeUnpin for BackgroundTasks
impl UnwindSafe for BackgroundTasks
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Component for T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more