Comments (5)
I'm in agreement with, @andrewbanchich
from async-task.
We could likely model this similar to the std::thread
"parking" APIs, but with the benefit that unlike Thread
futures permit external parking as well. For a counterpart to crossbeam's Parker/Unparker
I've been experimenting with this here. Luckily this is executor-agnostic, though it's not yet implemented directly in terms of atomics and may be subject to races. But at least the shape of the API seems alright.
Proposed API
impl Task {
/// Stop execution of the `Task` until `unpark` is called.
///
/// If the task was already parked this has no effect.
// NOTE: this method does not exist on `std::thread::Thread`
// NOTE: should this be an `async fn`?
pub fn park(&self);
/// Resume execution of the `Task` if it was parked.
///
/// If the task was not parked this has no effect.
pub fn unpark(&self);
}
/// Suspend execution of the current task.
// NOTE: we're not always guaranteed to be in a task. What should happen if we detect we aren't?
pub async fn park();
from async-task.
I feel as the proposed fn is a. Necessity. While I currently have nothing further to contribute with respect to "suggestions" regarding the implementation(s) of the API struct--I am very interested in the proposed-format and I will definitely spend more time with it to evaluate any direct/indirect- pros and cons and then will follow back up with results. The example here is great I'm fired up about the notion personally. Thank you, @yoshuawuyts!
REF: "test parking/unpacking #13"
from async-task.
I feel like this shouldn't be a part of this crate. You could model a "pausable future" using something like this:
struct PausableFuture<F> {
fut: F,
paused: Arc<AtomicBool>,
}
struct PauseHandle {
paused: Arc<AtomicBool>,
}
// new() takes F, creates paused, clones it, then returns both of the above types
impl PauseHandle {
fn pause(&self) {
self.paused.store(true);
}
fn unpause(&self) {
self.paused.store(false);
}
}
impl Future for PausedFuture<F> {
fn poll() {
if self.paused.load() {
Poll::Pending
} else {
self.fut.poll()
}
}
}
You'd also need to deal with wakers and such, but this can easily be done without this crate.
from async-task.
I've recently been working on a new model for tasks. Instead of "a task is an async thread" it's now: "a task is a parallelizable future". And all concurrency operations available on Task
should be available on Future
as well.
I've successfully authored a FutureExt::park method which implements these semantics. And I think it's probably best if we evolve that in a crate other than async-task
.
from async-task.
Related Issues (20)
- Add blocking wrapper HOT 6
- How to join the task when task sets detached? HOT 2
- Run `poll_drop` destructors when invoking `Task::cancel` HOT 2
- Option to avoid panicking when polling a cancelled task HOT 3
- Task not get rescheduled randomly HOT 1
- Can there be no additional scheduling of tasks when Task cancel or Detach HOT 1
- Custom allocators HOT 3
- How can I clone a `Task` HOT 6
- spawn_local for no_std ? HOT 4
- Add names to tasks HOT 1
- Dummy task HOT 2
- Bug: Unit test with expected panic message randomly fails because of crate-specific panic message HOT 2
- Why was metadata removed from a previous version of async-task? HOT 2
- Re-using memory for RawTask HOT 1
- Run tests for async-executor in CI
- Http clients for async-task HOT 5
- Build fails on nightly due to type_length_limit error HOT 1
- Support for statically allocating tasks (no_alloc usage) HOT 4
- Error in docs about `Runnable::run` return value HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-task.