How to spawn an async task that races freely with the current task


Suppose I have an existing async fn that handles a web request for a user. I want to change it to fire off a new network request—some kind of notification, for debugging or logging. I do not want to block the user on this extra request.

Can I do that with futures_api and async_await?

In C# and JS, calling an async method automatically schedules the task to run whether anyone ever awaits the result or not. Python doesn’t do that (like Rust, by default nothing happens until you await), but I can schedule a task that races freely with the current task, using asyncio.create_task. But I don’t see a way to do this at all in Rust.




To call that, I need a mut reference to the current executor. Is there a way for an async task to get that?

Also, is the futures crate compatible with features(async_await)?


Sorry posted too quick. I’m too out of date with the changes. There used to be the spawn! macro, not figured out where it’s gone.

It is futures_preview crate that keeps up with async.

1 Like

There is no builtin way to get a reference to an executor, since executors are not (yet) standardized. Which means you have to forward your used executor referenced to wherever it is required.


futures-0.3.0-alphaXX are compatible with async_await. They also provide a compat feature for converting futures from 0.1 to std futures and back. It can all be a bit confusing at first, don’t hesitate to ask. In my toml files it looks something like this, appearantly in this particular project I haven’t even needed the compat feature:

futures-preview      = "0.3.0-alpha.13"
futures-util-preview = "0.3.0-alpha.13"
futures-locks        = "0.3.3"
tokio                = { version = "~0.1.15", features = ["async-await-preview"] }
tokio-async-await    = "0.1.5"
tokio-uds            = "0.2.5"

It’s a bit difficult to answer that, since it really depends on your code and how you create that executor… I’m mainly using actix which has Arbiter::spawn, which can literally be called from anywhere. I think tokio::spawn_async is equally convenient, but I’m not sure since I don’t often use it.