async-std icon indicating copy to clipboard operation
async-std copied to clipboard

Adjust the Global Executor thread count

Open deankarn opened this issue 6 years ago • 3 comments

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 :)

deankarn avatar Nov 27 '19 17:11 deankarn

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 avatar Dec 12 '19 14:12 yoshuawuyts

@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 😃

deankarn avatar Dec 12 '19 15:12 deankarn

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.

ozgrakkurt avatar Mar 22 '23 20:03 ozgrakkurt