Skip to content

Commit 01b247e

Browse files
author
Andrew J Westlake
committed
added support for a ThreadPoolExecutor for long blocking tasks, overrides default executor to allow graceful shutdown
1 parent 1c3e70c commit 01b247e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ lazy_static! {
2424
}
2525

2626
static EVENT_LOOP: OnceCell<PyObject> = OnceCell::new();
27+
static EXECUTOR: OnceCell<PyObject> = OnceCell::new();
2728
static CALL_SOON: OnceCell<PyObject> = OnceCell::new();
2829
static CREATE_TASK: OnceCell<PyObject> = OnceCell::new();
2930
static CREATE_FUTURE: OnceCell<PyObject> = OnceCell::new();
@@ -34,12 +35,16 @@ static CREATE_FUTURE: OnceCell<PyObject> = OnceCell::new();
3435
pub fn try_init(py: Python) -> PyResult<()> {
3536
let asyncio = py.import("asyncio")?;
3637
let event_loop = asyncio.call_method0("get_event_loop")?;
38+
let executor = py.import("concurrent.futures.thread")?.getattr("ThreadPoolExecutor")?.call0()?;
39+
40+
event_loop.call_method1("set_default_executor", (executor,))?;
3741

3842
let call_soon = event_loop.getattr("call_soon_threadsafe")?;
3943
let create_task = asyncio.getattr("run_coroutine_threadsafe")?;
4044
let create_future = event_loop.getattr("create_future")?;
4145

4246
EVENT_LOOP.get_or_init(|| event_loop.into());
47+
EXECUTOR.get_or_init(|| executor.into());
4348
CALL_SOON.get_or_init(|| call_soon.into());
4449
CREATE_TASK.get_or_init(|| create_task.into());
4550
CREATE_FUTURE.get_or_init(|| create_future.into());
@@ -87,7 +92,15 @@ where
8792
}
8893

8994
pub fn close(py: Python) -> PyResult<()> {
95+
// Shutdown the executor and wait until all threads are cleaned up
96+
EXECUTOR.get().unwrap().call_method0(py, "shutdown")?;
97+
98+
EVENT_LOOP
99+
.get()
100+
.unwrap()
101+
.call_method0(py, "stop")?;
90102
EVENT_LOOP.get().unwrap().call_method0(py, "close")?;
103+
91104
Ok(())
92105
}
93106

0 commit comments

Comments
 (0)