Parasitic Task
Parasitic tasks are tasks that are not scheduled by any executor, but are instead directly dispatched at once on the thread that invokes the waker.
This makes it possible to process asynchronous results just-in-place, without the need for queueing the subsequent operations on a separate thread. This is useful for situations where response latency is a critical issue, such as in high-frequency trading applications.
Usage
Simply run a parasitic task with ptask::spawn which is immediately dispatched on the current thread, until it awaits for some future that has not yet completed, typically receiving from an asynchronous channel.
Note that the task is ref-counted with Arc internally, as the asynchonous operations that are awaited must be holding the waker that keeps the task alive. Thus, the sender could be regarded as the owner of the task that is awaiting for the receiver. Keep in mind that if there are multiple ptasks that holds the senders of each other, some senders has to be wrapped within Weak references to avoid circular references.
use ;
use ;
// This demo shows how it works on a single thread.
Capabilities
This crate supports no_std but requires the alloc crate to be available.
Optional features
This crate by default marks all functions as #[inline(always)], which ensures extreme performance in desired cases. However, if code size is still a concern, you can disable this behaviour by disabling the default features, which inlines like normal crates.