async-std
async-std copied to clipboard
Adjust the Global Executor thread count
I was wondering if there is any ability, or plans, to support setting the maximum number of threads for the global executor?
I see the num_cpus is used here which is great 99% of the time, my use case, however, is for docker.
num_cpus can/will show more CPU's than is allocated for the container to use. I was hoping to be able to directly control this for situations like this.
Thanks you in advance for any information you can provide :)
Hey there! Thanks for asking. We definitely want to provide such controls, but it's a question of how we should go about this. One option that's been mentioned is passing it as a global variable. But we haven't really talked about this much, primarily also because setting max threads can be risky (deadlocks!), and setting boundaries in terms of cpu cycles and memory usage tends to be a better option in practice.
@yoshuawuyts I completely agree that in practice most of the time this is a sane default, I just hope that the option to tweak at this low level is not abstracted away so far that it is not tunable and glad it's being thought about 😃
Hey @deankarn,
this issue seems to be solved.
Tracking how async-std handles thread count limits:
- https://github.com/async-rs/async-std/blob/bf316b095c176c8738c6401cc62d0bc389c88961/src/rt/mod.rs#L12
- https://github.com/async-rs/async-global-executor/blob/0abe723db4ad440f5cebbd06f95b6234a8116398/src/config.rs#L62
- https://doc.rust-lang.org/stable/std/thread/fn.available_parallelism.html
So it seems to use this by default. And it seems to be controllable by ASYNC_STD_THREAD_COUNT env var.
Apparently this doesnt cover spawn_blocking calls. Those seem like they can be controlled by BLOCKING_MAX_THREADS env var. I found it from https://github.com/async-rs/async-global-executor/issues/4.