# \`async\` for functions spawning tasks?

**URL:** <https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243>\
**Category:** help\
**Created:** [July 12, 2021, 11:34am UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243 "2021-07-12T11:34:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![farnbams](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/farnbams/32/12759_2.png) [@farnbams](https://users.rust-lang.org/u/farnbams)\
**Post date:** [July 12, 2021, 11:34am UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243/1 "2021-07-12T11:34:04Z")

</div>

Hi all,

I have a design question. Assume we have a struct that upon creation spawns an `async` task, e.g.:

```rust
struct Handle(Sender<u32>)

impl Handle {
    fn new() {
        let (rx, tx) = channel();
        tokio::task::spawn(async { 
            while Ok(num) = rx.recv().await {
                println!("Got {}", num);
            } 
        });
        Self(tx)
    }

    fn print(num: u32) { self.0.send(num).ok() }
}

```

Now `Handle::new()` is not a future but it will panic if there is no executor running in the background. Would you declare `new()` as async to prevent a panic or leave it as a 'normal' function?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [July 12, 2021, 11:37am UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243/2 "2021-07-12T11:37:52Z")

</div>

It's best to declare functions as `async fn` to avoid forcing a particular runtime on the callers.

For example, even though I use tokio, I have two runtimes with different configurations, and I choose what goes on which one of them. I don't want any library to call `spawn` ever.

If you must be spawning things ad-hoc yourself, take a `Handle`.

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [July 12, 2021, 12:04pm UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243/3 "2021-07-12T12:04:37Z")

</div>

I don't understand how declaring it async would help here.

---

<div class="post-metadata">

**Author:** ![farnbams](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/farnbams/32/12759_2.png) [@farnbams](https://users.rust-lang.org/u/farnbams)\
**Post date:** [July 12, 2021, 12:07pm UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243/4 "2021-07-12T12:07:41Z")

</div>

Well, having it `async` would be a more clear way of expressing 'You need some kind of executor to run this'. However, the more I think about it the less beneficial it seems to be... 😅

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [July 12, 2021, 12:18pm UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243/5 "2021-07-12T12:18:23Z")

</div>

Well, the way your method is implemented, you need Tokio specifically.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [October 10, 2021, 12:18pm UTC](https://users.rust-lang.org/t/async-for-functions-spawning-tasks/62243/6 "2021-10-10T12:18:27Z")

</div>

Closed on October 10, 2021, 12:18pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
