forked from lance-format/lance-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.rs
More file actions
176 lines (161 loc) · 5.74 KB
/
Copy pathexecutor.rs
File metadata and controls
176 lines (161 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2023 Lance Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::sync::mpsc::RecvTimeoutError;
use futures::Future;
use pyo3::{exceptions::PyRuntimeError, PyResult, Python};
pub const SIGNAL_CHECK_INTERVAL: std::time::Duration = std::time::Duration::from_millis(100);
/// A wrapper around tokio runtime.
///
/// This is used to spawn tasks in the background and wait synchronously for them
/// to complete. This is important for cases where we want to avoid nested
/// block_on() calls.
///
/// The methods also make sure that the GIL is released before spawning the task.
pub struct BackgroundExecutor {
pub runtime: tokio::runtime::Runtime,
}
impl BackgroundExecutor {
#[allow(dead_code)]
pub fn get_runtime_handle(&self) -> Option<tokio::runtime::Handle> {
Some(self.runtime.handle().clone())
}
/// Creates a tokio runtime and spawns a thread to run it.
pub fn new() -> Self {
// Create a new Runtime to run tasks
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_name("lance_background_thread")
.build()
.expect("Creating Tokio runtime");
Self { runtime }
}
/// Spawn a task and wait for it to complete.
///
/// This method is safe to use with inputs that may reference a Rust async
/// runtime.
#[allow(dead_code)]
pub fn spawn<T>(&self, py: Option<Python<'_>>, task: T) -> PyResult<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
if let Some(py) = py {
py.allow_threads(|| self.spawn_impl(task))
} else {
// Python::with_gil is a no-op if the GIL is already held by the thread.
Python::with_gil(|py| py.allow_threads(|| self.spawn_impl(task)))
}
}
#[allow(dead_code)]
fn spawn_impl<T>(&self, task: T) -> PyResult<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
let (tx, rx) = std::sync::mpsc::channel::<T::Output>();
let fut = Box::pin(async move {
let task_output = task.await;
tokio::task::spawn_blocking(move || {
tx.send(task_output).ok();
})
.await
.unwrap();
});
let handle = self.runtime.spawn(fut);
loop {
// Check for keyboard interrupts
match Python::with_gil(|py| py.check_signals()) {
Ok(_) => {}
Err(err) => {
handle.abort();
return Err(err);
}
}
// Wait for 100ms before checking signals again
match rx.recv_timeout(SIGNAL_CHECK_INTERVAL) {
Ok(output) => return Ok(output),
Err(RecvTimeoutError::Timeout) => continue,
Err(RecvTimeoutError::Disconnected) => {
handle.abort();
return Err(PyRuntimeError::new_err("Task was aborted"));
}
}
}
}
/// Spawn a task in the background
#[allow(dead_code)]
pub fn spawn_background<T>(&self, py: Option<Python<'_>>, task: T)
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
if let Some(py) = py {
py.allow_threads(|| {
self.runtime.spawn(task);
})
} else {
// Python::with_gil is a no-op if the GIL is already held by the thread.
Python::with_gil(|py| {
py.allow_threads(|| {
self.runtime.spawn(task);
})
})
}
}
/// Block on a future and wait for it to complete.
///
/// This helper method also frees the GIL before blocking.
///
/// This method is NOT safe to use with inputs that may reference a Rust async
/// runtime. If the future references an async runtime, it will panic on an
/// error: "Cannot start a runtime from within a runtime."
pub fn block_on<F: Future + Send>(
&self,
py: Option<Python<'_>>,
future: F,
) -> PyResult<F::Output>
where
F::Output: Send,
{
let future = Self::result_or_interrupt(future);
if let Some(py) = py {
py.allow_threads(move || self.runtime.block_on(future))
} else {
// Python::with_gil is a no-op if the GIL is already held by the thread.
Python::with_gil(|py| py.allow_threads(|| self.runtime.block_on(future)))
}
}
async fn result_or_interrupt<F>(future: F) -> PyResult<F::Output>
where
F: Future + Send,
F::Output: Send,
{
let interrupt_future = async {
loop {
// Check for keyboard interrupts
match Python::with_gil(|py| py.check_signals()) {
Ok(_) => {
// Wait for 100ms before checking signals again
tokio::time::sleep(SIGNAL_CHECK_INTERVAL).await;
}
Err(err) => return Err(err),
}
}
};
tokio::select! {
result = future => Ok(result),
err = interrupt_future => err,
}
}
}