Crates.io | stateful_async_worker |
lib.rs | stateful_async_worker |
version | 0.1.2 |
source | src |
created_at | 2020-07-30 21:46:11.736468 |
updated_at | 2021-02-21 10:29:51.888287 |
description | Asynchronous stateful worker threads utilizable as futures |
homepage | https://github.com/tom-heimbrodt/stateful_async_worker |
repository | https://github.com/tom-heimbrodt/stateful_async_worker |
max_upload_size | |
id | 271470 |
size | 18,144 |
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.
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
}