#worker #future #async #threading

stateful_async_worker

Asynchronous stateful worker threads utilizable as futures

3 releases

0.1.2 Feb 21, 2021
0.1.1 Jul 30, 2020
0.1.0 Jul 30, 2020

#1110 in Asynchronous

MIT license

17KB
293 lines

Rust Docs.rs Crates.io Crates.io License

Stateful async worker provides worker thread structures to allow the execution of synchronous functions or closures in the background and asynchronously access the result as future.

To execute multiple functions at the same time ThreadPool is helpful. For inherently single threaded operations like disk I/O WorkerThread should be sufficient.

Example

   use stateful_async_worker::WorkerThread;

   async fn example() -> u64 {
       // Create a worker thread that wraps a number.
       let worker = WorkerThread::spawn_with(0u64);

       // Now you can run closures as futures in the background!
       let add_three = worker.work_on(|num: &mut u64| {
           // Do some sophisticated computations here ...
           *num += 3;
           *num
       });
       add_three.await
   }

Dependencies

~1.5MB
~23K SLoC