Skip to content

Commit b2f9da8

Browse files
committed
add async_call rustpython
1 parent 307bef9 commit b2f9da8

File tree

5 files changed

+260
-24
lines changed

5 files changed

+260
-24
lines changed

Cargo.lock

Lines changed: 163 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tokio = { version = "1.47.0", features = ["sync", "macros", "rt", "rt-multi-thre
2121
default = ["pyo3"]
2222
# default = ["rustpython"]
2323
pyo3 = ["dep:pyo3"]
24-
rustpython = ["dep:rustpython-vm", "dep:rustpython-stdlib"]
24+
rustpython = ["dep:rustpython-vm", "dep:rustpython", "dep:rustpython-stdlib"]
2525

2626
[dependencies.pyo3]
2727
version = "0.26.0"
@@ -33,6 +33,10 @@ version = "0.4.0"
3333
optional = true
3434
features = ["threading", "serde", "importlib"]
3535

36+
[dependencies.rustpython]
37+
version = "0.4.0"
38+
optional = true
39+
3640
[dependencies.rustpython-stdlib]
3741
version = "0.4.0"
3842
optional = true

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ pub struct PyRunner {
9292
sender: mpsc::Sender<PyCommand>,
9393
}
9494

95-
9695
impl Default for PyRunner {
9796
fn default() -> Self {
9897
PyRunner::new()
@@ -124,7 +123,9 @@ impl PyRunner {
124123

125124
#[cfg(feature = "rustpython")]
126125
{
127-
rustpython_runner::python_thread_main(receiver);
126+
use tokio::runtime::Builder;
127+
let rt = Builder::new_current_thread().enable_all().build().unwrap();
128+
rt.block_on(rustpython_runner::python_thread_main(receiver));
128129
}
129130
});
130131

src/pyo3_runner.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ pub(crate) async fn python_thread_main(mut receiver: mpsc::Receiver<PyCommand>)
4848

4949
match result {
5050
Ok(func) => {
51-
py.detach(|| tokio::spawn(handle_call_async_function(func, args, cmd.responder)));
51+
py.detach(|| {
52+
tokio::spawn(handle_call_async_function(func, args, cmd.responder))
53+
});
5254
return; // The response is sent async, so we can return early.
5355
}
5456
Err(e) => Err(e),
@@ -190,8 +192,7 @@ fn py_any_to_json(obj: &pyo3::Bound<'_, PyAny>) -> PyResult<Value> {
190192
return Ok(Value::String(s.to_string()));
191193
}
192194
if let Ok(list) = obj.cast::<PyList>() {
193-
let items: PyResult<Vec<Value>> =
194-
list.iter().map(|item| py_any_to_json(&item)).collect();
195+
let items: PyResult<Vec<Value>> = list.iter().map(|item| py_any_to_json(&item)).collect();
195196
return Ok(Value::Array(items?));
196197
}
197198
if let Ok(dict) = obj.cast::<PyDict>() {

0 commit comments

Comments
 (0)