# Tokio: spawn vs spawn\_local

**URL:** <https://users.rust-lang.org/t/tokio-spawn-vs-spawn-local/62047>\
**Category:** help\
**Created:** [July 8, 2021, 10:48am UTC](https://users.rust-lang.org/t/tokio-spawn-vs-spawn-local/62047 "2021-07-08T10:48:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lambdakappatheta](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lambdakappatheta](https://users.rust-lang.org/u/lambdakappatheta)\
**Post date:** [July 8, 2021, 10:48am UTC](https://users.rust-lang.org/t/tokio-spawn-vs-spawn-local/62047/1 "2021-07-08T10:48:09Z")

</div>

If I create a top-level future inside which I `spawn` some tasks, those tasks continue to execute even after the call to `block_on` that drives the top-level future returns:

```rust
use tokio::runtime::Runtime;
use tokio::spawn;
use tokio::time::sleep;

use std::time::Duration;

fn main() {
    let rt = Runtime::new().unwrap();

    rt.block_on(async {
        spawn(async {
            loop {
                println!("a");
                sleep(Duration::from_secs(2)).await;
            }
        });
    });

    println!("done");

    loop {}
}

```

However tasks spawned using `spawn_local` seem to stop executing right after `block_on` returns:

```rust
use tokio::runtime::Runtime;
use tokio::task::{spawn_local, LocalSet};
use tokio::time::sleep;

use std::time::Duration;

fn main() {
    let rt = Runtime::new().unwrap();
    let local = LocalSet::new();

    local.block_on(&rt, async {
        spawn_local(async {
            loop {
                println!("a");
                sleep(Duration::from_secs(2)).await;
            }
        });
    });

    println!("done");

    loop {}
}

```

And the same thing happens with `run_until`:

```rust
use tokio::runtime::Runtime;
use tokio::task::{spawn_local, LocalSet};
use tokio::time::sleep;

use std::time::Duration;

fn main() {
    let rt = Runtime::new().unwrap();
    let local = LocalSet::new();

    rt.block_on(local.run_until(async {
        spawn_local(async {
            loop {
                println!("a");
                sleep(Duration::from_secs(2)).await;
            }
        });
    }));

    println!("done");

    loop {}
}

```

By the way, `async-std` seems to behave like `tokio` in this respect.

So what's the reason for this difference between `spawn` and `spawn_local`?

---

<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 8, 2021, 12:32pm UTC](https://users.rust-lang.org/t/tokio-spawn-vs-spawn-local/62047/2 "2021-07-08T12:32:44Z")

</div>

This is because a `LocalSet` behaves like a [`current_thread`](https://docs.rs/tokio/1/tokio/runtime/struct.Builder.html#method.new_current_thread) runtime in this regard. See the following quote from the Tokio tutorial:

> Because the `current_thread` runtime does not spawn threads, it only operates when `block_on` is called. Once `block_on` returns, all spawned tasks on that runtime will freeze until you call `block_on` again. Use the `multi_threaded` runtime if spawned tasks must keep running when not calling `block_on` .
> 
> [Bridging with sync code](https://tokio.rs/tokio/tutorial/bridging)

One way to see the issue is to consider the question "where should the local task run?". It certainly can't run on any other thread than your main thread, as that would be in violation with the task not being `Send`, and the main thread is currently occupied on looping really quickly.

---

<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 6, 2021, 12:33pm UTC](https://users.rust-lang.org/t/tokio-spawn-vs-spawn-local/62047/3 "2021-10-06T12:33:15Z")

</div>

Closed on October 6, 2021, 12:33pm 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.
