Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ categories = ["api-bindings", "asynchronous", "external-ffi-bindings"]
dunce = "1.0.4"
serde_json = "1.0.114"
thiserror = "2.0"
tokio = { version = "1.36.0", features = ["sync", "macros"] }
once_cell = "1.19"
tokio = { version = "1.47.0", features = ["sync", "macros", "rt", "rt-multi-thread"] }

[features]
default = ["pyo3"]
Expand All @@ -27,18 +28,16 @@ version = "0.26.0"
features = ["auto-initialize"]
optional = true

[dev-dependencies]
tempfile = "3"
[dependencies.rustpython-vm]
version = "0.4.0"
optional = true
features = ["threading", "serde", "importlib"]

[dependencies.rustpython-stdlib]
version = "0.4.0"
optional = true
features = ["threading"]

# The `full` feature for tokio is needed for the tests
[dev-dependencies.tokio]
version = "1"
features = ["full"]
[dev-dependencies]
tempfile = "3"
tokio = { version = "1.47.0", features = ["full"] }
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ def greet(name):
println!("{}", result2.as_str().unwrap()); // Prints: Hello World! Called 2 times from Python.
}
```

### Async Python Example

```rust
use async_py::PyRunner;

#[tokio::main]
async fn main() {
let runner = PyRunner::new();
let code = r#"
import asyncio
counter = 0
async def add_and_sleep(a, b, sleep_time):
global counter
await asyncio.sleep(sleep_time)
counter += 1
return a + b + counter
"#;

runner.run(code).await.unwrap();
let result1 = runner.call_async_function("add_and_sleep", vec![5.into(), 10.into(), 1.into()]);
let result2 = runner.call_async_function("add_and_sleep", vec![5.into(), 10.into(), 0.1.into()]);
let (result1, result2) = tokio::join!(result1, result2);
assert_eq!(result1.unwrap(), Value::Number(17.into()));
assert_eq!(result2.unwrap(), Value::Number(16.into()));
}
```
Both function calls are triggered to run async code at the same time. While the first call waits for the sleep,
the second can already start and also increment the counter first. Therefore,
result1 will wait longer and compute 5 + 10 + 2, while the result2 can compute 5 + 10 + 1.

Each call will use its own event loop. This may not be very efficient and changed later.

Make sure to use `call_async_function` for async python functions. Using `call_function` will
probably raise an error.
`call_async_function` is not available for RustPython.

### Using a venv
It is generally recommended to use a venv to install pip packages.
While you cannot switch the interpreter version with this crate, you can use an
Expand Down
Loading