Skip to content

Allow for bringing your own smol executor #4273

@RustyNova016

Description

@RustyNova016

I have found these related issues/pull requests

Description

The current implementation of the smol runtime is calling smol::spawn(), which is a fancy global variable. However, consumers of the crate may want to create their own executor to fix some problems with this api:

  • Number of threads are set at runtime, and not at compile time by the developer that knows better.
  • Requires an environment variable for the number of threads
  • the global executor is hidden and cannot be accessed directly

Allowing to set your own executor would be better as you could personalize it to your desires

Prefered solution

One way i'd fix it is:

  • create a static SMOL_EXECUTOR: Arc<Mutex<Option<Executor<'_>>>>
  • create a fn set_smol_executor() function to allow setting the executor without dealing with the mutex
  • Replace smol::spawn by something like this:
if let Some(exec) = SMOL_EXECUTOR.lock() {
   exec.spawn(fut)
else {
   smol::spawn(fut)
}

I am willing to implement it if greenlit

Is this a breaking change? Why or why not?

We're only adding here :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions