tikv / yatp

Yet another thread pool in rust for both callbacks or futures.
Apache License 2.0
135 stars 32 forks source link

A simple way to create custom executors #41

Open ghost opened 4 years ago

ghost commented 4 years ago

Hello! I recently made crate named async-executor that makes it quite easy to create custom executors with almost arbitrary task scheduling strategies. Sharing this here because I thought you mind find it useful. In fact, I noticed tikv/yatp a long time ago and had it specifically in mind when researching how to make executors simpler and more flexible :)

https://docs.rs/async-executor/0.2.1/async_executor/

The interface is very simple. The Executor type essentially has the following methods:

The first method spawns a new task, and the second method runs a single scheduled task. This API is pretty flexible because it allows you to compose multiple executors in interesting ways.

As a proof of concept, I made an executor with three task priorities (high, medium, low). Implementation:

https://github.com/stjepang/async-executor/blob/master/examples/priority.rs

It's not a lot of code at all! And it's fairly straightforward to extend this executor with new capabilities. For example, we could run lower priority tasks every 100 ms or so in order to ensure they don't get starved forever.

BusyJay commented 4 years ago

Thanks for recommendation! It's a fantastic library! In yatp, tasks don't have fixed priority, which doesn't seem to fit in the model though.