mirror of https://github.com/stjepang/smol
Merge pull request #205 from Keruspe/mt-executor
allow spawning more global executor threads
This commit is contained in:
commit
c041d946eb
27
src/lib.rs
27
src/lib.rs
|
@ -79,9 +79,11 @@ pub mod prelude {
|
||||||
|
|
||||||
/// Spawns a task onto the single-threaded global executor.
|
/// Spawns a task onto the single-threaded global executor.
|
||||||
///
|
///
|
||||||
/// There is a single-threaded global executor that gets lazily initialized on first use. It is
|
/// There is a (single-threaded by default) global executor that gets lazily initialized on first use.
|
||||||
/// advisable to use it in tests or small programs, but it is otherwise a better idea to define
|
/// It is advisable to use it in tests or small programs, but it is otherwise a better idea to define
|
||||||
/// your own [`Executor`]s.
|
/// your own [`Executor`]s.
|
||||||
|
/// You can configure the number of threads of the global executor using the `SMOL_THREADS` environment
|
||||||
|
/// variable.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -96,15 +98,22 @@ pub mod prelude {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
|
pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
|
||||||
static GLOBAL: Lazy<Executor> = Lazy::new(|| {
|
static GLOBAL: Lazy<Executor> = Lazy::new(|| {
|
||||||
thread::Builder::new()
|
let num_threads = {
|
||||||
.name("smol".to_string())
|
// Parse SMOL_THREADS or run a monothreaded executor.
|
||||||
.spawn(|| {
|
std::env::var("SMOL_THREADS")
|
||||||
loop {
|
.ok()
|
||||||
|
.and_then(|s| s.parse().ok())
|
||||||
|
.unwrap_or(1)
|
||||||
|
};
|
||||||
|
for n in 1..=num_threads {
|
||||||
|
thread::Builder::new()
|
||||||
|
.name(format!("smol-{}", n))
|
||||||
|
.spawn(|| loop {
|
||||||
let _ =
|
let _ =
|
||||||
catch_unwind(|| async_io::block_on(GLOBAL.run(future::pending::<()>())));
|
catch_unwind(|| async_io::block_on(GLOBAL.run(future::pending::<()>())));
|
||||||
}
|
})
|
||||||
})
|
.expect("cannot spawn executor thread");
|
||||||
.unwrap();
|
}
|
||||||
Executor::new()
|
Executor::new()
|
||||||
});
|
});
|
||||||
GLOBAL.spawn(future)
|
GLOBAL.spawn(future)
|
||||||
|
|
Loading…
Reference in New Issue