Skip to content

task::block_on -> thread::spawn_task #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ task::blocking(async {

- Refactored the scheduling algorithm of our executor to use work stealing
- Refactored the network driver, removing 400 lines of code
- Removed the `Send` bound from `task::block_on`
- Removed the `Send` bound from `thread::spawn_task`
- Removed `Unpin` bound from `impl<T: futures::stream::Stream> Stream for T`

# [0.99.5] - 2019-09-12
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $ cargo add async-std
use async_std::task;

fn main() {
task::block_on(async {
thread::spawn_task(async {
println!("Hello, world!");
})
}
Expand Down Expand Up @@ -72,7 +72,7 @@ async fn get() -> io::Result<Vec<u8>> {
}

fn main() {
task::block_on(async {
thread::spawn_task(async {
let raw_response = get().await.expect("request");
let response = String::from_utf8(raw_response)
.expect("utf8 conversion");
Expand Down
2 changes: 1 addition & 1 deletion benches/task_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn get(b: &mut Bencher) {
}

let mut sum = 0;
task::block_on(async {
thread::spawn_task(async {
b.iter(|| VAL.with(|v| sum += v));
});
black_box(sum);
Expand Down
8 changes: 4 additions & 4 deletions docs/src/concepts/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
}
});
println!("Started task!");
task::block_on(reader_task);
thread::spawn_task(reader_task);
println!("Stopped task!");
}
```
Expand Down Expand Up @@ -86,7 +86,7 @@ Tasks in `async_std` are one of the core abstractions. Much like Rust's `thread`
# extern crate async_std;
# use async_std::task;
fn main() {
task::block_on(async {
thread::spawn_task(async {
// this is std::fs, which blocks
std::fs::read_to_string("test_file");
})
Expand All @@ -107,7 +107,7 @@ In practice, that means that `block_on` propagates panics to the blocking compon
# extern crate async_std;
# use async_std::task;
fn main() {
task::block_on(async {
thread::spawn_task(async {
panic!("test");
});
}
Expand All @@ -128,7 +128,7 @@ task::spawn(async {
panic!("test");
});

task::block_on(async {
thread::spawn_task(async {
task::sleep(Duration::from_millis(10000)).await;
})
```
Expand Down
4 changes: 2 additions & 2 deletions docs/src/tutorial/accept_loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ Finally, let's add main:
// main
fn run() -> Result<()> {
let fut = accept_loop("127.0.0.1:8080");
task::block_on(fut)
thread::spawn_task(fut)
}
```

The crucial thing to realise that is in Rust, unlike other languages, calling an async function does **not** run any code.
Async functions only construct futures, which are inert state machines.
To start stepping through the future state-machine in an async function, you should use `.await`.
In a non-async function, a way to execute a future is to hand it to the executor.
In this case, we use `task::block_on` to execute a future on the current thread and block until it's done.
In this case, we use `thread::spawn_task` to execute a future on the current thread and block until it's done.
2 changes: 1 addition & 1 deletion docs/src/tutorial/all_together.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Receiver<T> = mpsc::UnboundedReceiver<T>;

// main
fn run() -> Result<()> {
task::block_on(accept_loop("127.0.0.1:8080"))
thread::spawn_task(accept_loop("127.0.0.1:8080"))
}

fn spawn_and_log_error<F>(fut: F) -> task::JoinHandle<()>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/tutorial/handling_disconnection.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ enum Void {}

// main
fn run() -> Result<()> {
task::block_on(accept_loop("127.0.0.1:8080"))
thread::spawn_task(accept_loop("127.0.0.1:8080"))
}

async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/tutorial/implementing_a_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>

// main
fn run() -> Result<()> {
task::block_on(try_run("127.0.0.1:8080"))
thread::spawn_task(try_run("127.0.0.1:8080"))
}

async fn try_run(addr: impl ToSocketAddrs) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions examples/a-chat/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use async_std::{
io::{stdin, BufReader},
net::{TcpStream, ToSocketAddrs},
prelude::*,
task,
thread,
};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

pub(crate) fn main() -> Result<()> {
task::block_on(try_main("127.0.0.1:8080"))
thread::spawn_task(try_main("127.0.0.1:8080"))
}

async fn try_main(addr: impl ToSocketAddrs) -> Result<()> {
Expand Down
3 changes: 2 additions & 1 deletion examples/a-chat/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use async_std::{
net::{TcpListener, TcpStream, ToSocketAddrs},
prelude::*,
task,
thread,
};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
Expand All @@ -20,7 +21,7 @@ type Receiver<T> = mpsc::UnboundedReceiver<T>;
enum Void {}

pub(crate) fn main() -> Result<()> {
task::block_on(accept_loop("127.0.0.1:8080"))
thread::spawn_task(accept_loop("127.0.0.1:8080"))
}

async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions examples/hello-world.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Spawns a task that says hello.

use async_std::task;
use async_std::thread;

async fn say_hi() {
println!("Hello, world!");
}

fn main() {
task::block_on(say_hi())
thread::spawn_task(say_hi())
}
4 changes: 2 additions & 2 deletions examples/line-count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::env::args;
use async_std::fs::File;
use async_std::io::{self, BufReader};
use async_std::prelude::*;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
let path = args().nth(1).expect("missing path argument");

task::block_on(async {
thread::spawn_task(async {
let file = File::open(&path).await?;
let mut lines = BufReader::new(file).lines();
let mut count = 0u64;
Expand Down
4 changes: 2 additions & 2 deletions examples/list-dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::env::args;
use async_std::fs;
use async_std::io;
use async_std::prelude::*;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
let path = args().nth(1).expect("missing path argument");

task::block_on(async {
thread::spawn_task(async {
let mut dir = fs::read_dir(&path).await?;

while let Some(res) = dir.next().await {
Expand Down
3 changes: 2 additions & 1 deletion examples/logging.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Prints the runtime's execution log on the standard output.

use async_std::thread;
use async_std::task;

fn main() {
femme::start(log::LevelFilter::Trace).unwrap();

task::block_on(async {
thread::spawn_task(async {
let handle = task::spawn(async {
log::info!("Hello world!");
});
Expand Down
4 changes: 2 additions & 2 deletions examples/print-file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use std::env::args;
use async_std::fs::File;
use async_std::io;
use async_std::prelude::*;
use async_std::task;
use async_std::thread;

const LEN: usize = 16 * 1024; // 16 Kb

fn main() -> io::Result<()> {
let path = args().nth(1).expect("missing path argument");

task::block_on(async {
thread::spawn_task(async {
let mut file = File::open(&path).await?;
let mut stdout = io::stdout();
let mut buf = vec![0u8; LEN];
Expand Down
5 changes: 3 additions & 2 deletions examples/socket-timeouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use std::time::Duration;

use async_std::{io, net::TcpStream, prelude::*, task};
use async_std::{io, net::TcpStream, prelude::*};
use async_std::thread;

async fn get() -> io::Result<Vec<u8>> {
let mut stream = TcpStream::connect("example.com:80").await?;
Expand All @@ -20,7 +21,7 @@ async fn get() -> io::Result<Vec<u8>> {
}

fn main() {
task::block_on(async {
thread::spawn_task(async {
let raw_response = get().await.expect("request");
let response = String::from_utf8(raw_response).expect("utf8 conversion");
println!("received: {}", response);
Expand Down
4 changes: 2 additions & 2 deletions examples/stdin-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use async_std::io;
use async_std::prelude::*;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
task::block_on(async {
thread::spawn_task(async {
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut line = String::new();
Expand Down
4 changes: 2 additions & 2 deletions examples/stdin-timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use std::time::Duration;

use async_std::io;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
// This async scope times out after 5 seconds.
task::block_on(io::timeout(Duration::from_secs(5), async {
thread::spawn_task(io::timeout(Duration::from_secs(5), async {
let stdin = io::stdin();

// Read a line from the standard input and display it.
Expand Down
4 changes: 2 additions & 2 deletions examples/surf-web.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* TODO: Once the next version of surf released, re-enable this example.
//! Sends an HTTP request to the Rust website.

use async_std::task;
use async_std::thread;

fn main() -> Result<(), surf::Exception> {
task::block_on(async {
thread::spawn_task(async {
let url = "https://www.rust-lang.org";
let mut response = surf::get(url).await?;
let body = response.body_string().await?;
Expand Down
4 changes: 2 additions & 2 deletions examples/task-local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use std::cell::Cell;

use async_std::prelude::*;
use async_std::task;
use async_std::thread;

task_local! {
static VAR: Cell<i32> = Cell::new(1);
}

fn main() {
task::block_on(async {
thread::spawn_task(async {
println!("var = {}", VAR.with(|v| v.get()));
VAR.with(|v| v.set(2));
println!("var = {}", VAR.with(|v| v.get()));
Expand Down
3 changes: 2 additions & 1 deletion examples/task-name.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Spawns a named task that prints its name.

use async_std::task;
use async_std::thread;

async fn print_name() {
println!("My name is {:?}", task::current().name());
}

fn main() {
task::block_on(async {
thread::spawn_task(async {
task::Builder::new()
.name("my-task".to_string())
.spawn(print_name())
Expand Down
4 changes: 2 additions & 2 deletions examples/tcp-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
use async_std::io;
use async_std::net::TcpStream;
use async_std::prelude::*;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
task::block_on(async {
thread::spawn_task(async {
let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
println!("Connected to {}", &stream.peer_addr()?);

Expand Down
3 changes: 2 additions & 1 deletion examples/tcp-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use async_std::io;
use async_std::net::{TcpListener, TcpStream};
use async_std::prelude::*;
use async_std::thread;
use async_std::task;

async fn process(stream: TcpStream) -> io::Result<()> {
Expand All @@ -21,7 +22,7 @@ async fn process(stream: TcpStream) -> io::Result<()> {
}

fn main() -> io::Result<()> {
task::block_on(async {
thread::spawn_task(async {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("Listening on {}", listener.local_addr()?);

Expand Down
4 changes: 2 additions & 2 deletions examples/udp-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

use async_std::io;
use async_std::net::UdpSocket;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
task::block_on(async {
thread::spawn_task(async {
let socket = UdpSocket::bind("127.0.0.1:8081").await?;
println!("Listening on {}", socket.local_addr()?);

Expand Down
4 changes: 2 additions & 2 deletions examples/udp-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

use async_std::io;
use async_std::net::UdpSocket;
use async_std::task;
use async_std::thread;

fn main() -> io::Result<()> {
task::block_on(async {
thread::spawn_task(async {
let socket = UdpSocket::bind("127.0.0.1:8080").await?;
let mut buf = vec![0u8; 1024];

Expand Down
2 changes: 1 addition & 1 deletion src/fs/canonicalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::task::blocking;
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async {
/// #
/// use async_std::fs;
///
Expand Down
2 changes: 1 addition & 1 deletion src/fs/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::task::blocking;
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async {
/// #
/// use async_std::fs;
///
Expand Down
2 changes: 1 addition & 1 deletion src/fs/create_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::task::blocking;
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async {
/// #
/// use async_std::fs;
///
Expand Down
Loading