Run async function in specific thread

Viewed 1562

I would like to run specific long-running functions (which execute database queries) on a separate thread. However, let's assume that the underlying database engine only allows one connection at a time and the connection struct isn't Sync (I think at least the latter is true for diesel).

My solution would be to have a single separate thread (as opposed to a thread pool) where all the database-work happens and which runs as long as the main thread is alive. I think I know how I would have to do this with passing messages over channels, but that requires quite some boilerplate code (e.g. explicitly sending the function arguments over the channel etc.).

Is there a more direct way of achieving something like this with rust (and possibly tokio and the new async/await notation that is in nightly)?

I'm hoping to do something along the lines of:

let handle = spawn_thread_with_runtime(...);

let future = run_on_thread!(handle, query_function, argument1, argument2);

where query_function would be a function that immediately returns a future and does the work on the other thread.

Rust nightly and external crates / macros would be ok.

1 Answers

If external crates are an option, I'd consider taking a look at actix, an Actor Framework for Rust.

This will let you spawn an Actor in a separate thread that effectively owns the connection to the DB. It can then listen for messages, execute work/queries based on those messages, and return either sync results or futures.

It takes care of most of the boilerplate for message passing, spawning, etc. at a higher level.

There's also a Diesel example in the actix documentation, which sounds quite close to the use case you had in mind.

Related