Skip to content

Unclear how to use CurrentThread and a "main future" #697

@alexcrichton

Description

@alexcrichton

A typical pattern I've seen with tokio-core is to do something like:

let srv = ...;

handle.spawn(another_future);
handle.spawn(another_future2);
// ...

let result = core.run(srv);

// work with `result`

The Handle::spawn functionality is getting folded into CurrentThread but unfortunately I'm not sure how to recover the let result = core.run(srv) functionality from CurrentThread. Currently the closures in CurrentThread both return a bare R, the value returned by the closure, and the future::blocking module doesn't allow usage of CurrentThread. As a result, I don't think it's currently easily possible to construct a server like this, where you've got one "main future" you're interested in and it may spawn (or already have spawned) some sub-futures it was interested in.

The current state of tokio-rs/tokio-rfcs#3 solves this use case with:

pub fn block_on_all<F: Future>(f: F)  -> Result<F::Item, F::Error>;

and the RFC's current text could be tweaked a bit to accommodate an initialization closure as well:

pub fn block_with_init<F, R>(f: F) -> Result<R::Item, R::Error> 
    where F: FnOnce(&Enter) -> R,
          R: IntoFuture;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions