Skip to content

Commit 9c9f299

Browse files
authored
Merge pull request #10 from awestlake87/attributes
Adding #[main] and #[test] attributes (issue #9)
2 parents 86dcfaf + 991c635 commit 9c9f299

20 files changed

+1099
-536
lines changed

Cargo.toml

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,42 @@ license = "Apache-2.0"
1212
exclude = ["/.gitignore", "/codecov.yml", "/Makefile"]
1313
edition = "2018"
1414

15-
16-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
15+
[workspace]
16+
members = ["pyo3-asyncio-macros"]
1717

1818
[features]
1919
async-std-runtime = ["async-std"]
20+
attributes = ["pyo3-asyncio-macros"]
2021
testing = ["clap"]
2122
tokio-runtime = ["tokio"]
2223
default = []
2324

25+
[[example]]
26+
name = "async_std"
27+
path = "examples/async_std.rs"
28+
required-features = ["attributes", "async-std-runtime"]
29+
30+
[[example]]
31+
name = "tokio"
32+
path = "examples/tokio.rs"
33+
required-features = ["attributes", "tokio-runtime"]
34+
35+
[[example]]
36+
name = "tokio_current_thread"
37+
path = "examples/tokio_current_thread.rs"
38+
required-features = ["attributes", "tokio-runtime"]
39+
40+
[[example]]
41+
name = "tokio_multi_thread"
42+
path = "examples/tokio_multi_thread.rs"
43+
required-features = ["attributes", "tokio-runtime"]
44+
45+
2446
[[test]]
2547
name = "test_async_std_asyncio"
2648
path = "pytests/test_async_std_asyncio.rs"
2749
harness = false
28-
required-features = ["async-std-runtime", "testing"]
50+
required-features = ["async-std-runtime", "testing", "attributes"]
2951

3052
[[test]]
3153
name = "test_async_std_run_forever"
@@ -37,7 +59,7 @@ required-features = ["async-std-runtime", "testing"]
3759
name = "test_tokio_current_thread_asyncio"
3860
path = "pytests/test_tokio_current_thread_asyncio.rs"
3961
harness = false
40-
required-features = ["tokio-runtime", "testing"]
62+
required-features = ["tokio-runtime", "testing", "attributes"]
4163

4264
[[test]]
4365
name = "test_tokio_current_thread_run_forever"
@@ -49,7 +71,7 @@ required-features = ["tokio-runtime", "testing"]
4971
name = "test_tokio_multi_thread_asyncio"
5072
path = "pytests/test_tokio_multi_thread_asyncio.rs"
5173
harness = false
52-
required-features = ["tokio-runtime", "testing"]
74+
required-features = ["tokio-runtime", "testing", "attributes"]
5375

5476
[[test]]
5577
name = "test_tokio_multi_thread_run_forever"
@@ -60,9 +82,11 @@ required-features = ["tokio-runtime", "testing"]
6082
[dependencies]
6183
clap = { version = "2.33", optional = true }
6284
futures = "0.3"
85+
inventory = "0.1"
6386
lazy_static = "1.4"
6487
once_cell = "1.5"
6588
pyo3 = "0.13"
89+
pyo3-asyncio-macros = { path = "pyo3-asyncio-macros", version = "=0.13.0", optional = true }
6690

6791
[dependencies.async-std]
6892
version = "1.9"

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ lint: fmt clippy
1414
test: lint
1515
cargo test --all-features
1616

17-
publish: test
17+
test-feature-powerset:
18+
cargo install cargo-hack
19+
cargo hack test --feature-powerset
20+
21+
publish: test-feature-powerset
1822
cargo publish

README.md

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,20 @@ Here we initialize the runtime, import Python's `asyncio` library and run the gi
2828

2929
More details on the usage of this library can be found in the [API docs](https://awestlake87.github.io/pyo3-asyncio/master/doc).
3030

31-
```rust no_run
31+
```rust
3232
use pyo3::prelude::*;
3333

34-
fn main() {
35-
Python::with_gil(|py| {
36-
// Initialize the runtime
37-
pyo3_asyncio::with_runtime(py, || {
38-
let asyncio: PyObject = py.import("asyncio")?.into();
39-
40-
// Run the event loop until the given future completes
41-
pyo3_asyncio::async_std::run_until_complete(py, async move {
42-
Python::with_gil(|py| {
43-
// convert asyncio.sleep into a Rust Future
44-
pyo3_asyncio::into_future(
45-
asyncio.call_method1(
46-
py,
47-
"sleep",
48-
(1.into_py(py),)
49-
)?
50-
.as_ref(py)
51-
)
52-
})?
53-
.await?;
54-
55-
Ok(())
56-
})?;
57-
58-
Ok(())
59-
})
60-
.map_err(|e| {
61-
e.print_and_set_sys_last_vars(py);
62-
})
63-
.unwrap();
64-
})
34+
#[pyo3_asyncio::async_std::main]
35+
async fn main() -> PyResult<()> {
36+
let fut = Python::with_gil(|py| {
37+
let asyncio = py.import("asyncio")?;
38+
39+
// convert asyncio.sleep into a Rust Future
40+
pyo3_asyncio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
41+
})?;
42+
43+
fut.await?;
44+
45+
Ok(())
6546
}
6647
```

examples/async_std.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyo3_asyncio::async_std::main]
4+
async fn main() -> PyResult<()> {
5+
let fut = Python::with_gil(|py| {
6+
let asyncio = py.import("asyncio")?;
7+
8+
// convert asyncio.sleep into a Rust Future
9+
pyo3_asyncio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
10+
})?;
11+
12+
println!("sleeping for 1s");
13+
fut.await?;
14+
println!("done");
15+
16+
Ok(())
17+
}

examples/tokio.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyo3_asyncio::tokio::main]
4+
async fn main() -> PyResult<()> {
5+
let fut = Python::with_gil(|py| {
6+
let asyncio = py.import("asyncio")?;
7+
8+
// convert asyncio.sleep into a Rust Future
9+
pyo3_asyncio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
10+
})?;
11+
12+
println!("sleeping for 1s");
13+
fut.await?;
14+
println!("done");
15+
16+
Ok(())
17+
}

examples/tokio_current_thread.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyo3_asyncio::tokio::main(flavor = "current_thread")]
4+
async fn main() -> PyResult<()> {
5+
let fut = Python::with_gil(|py| {
6+
let asyncio = py.import("asyncio")?;
7+
8+
// convert asyncio.sleep into a Rust Future
9+
pyo3_asyncio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
10+
})?;
11+
12+
println!("sleeping for 1s");
13+
fut.await?;
14+
println!("done");
15+
16+
Ok(())
17+
}

examples/tokio_multi_thread.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use pyo3::prelude::*;
2+
3+
#[pyo3_asyncio::tokio::main(flavor = "multi_thread", worker_threads = 10)]
4+
async fn main() -> PyResult<()> {
5+
let fut = Python::with_gil(|py| {
6+
let asyncio = py.import("asyncio")?;
7+
8+
// convert asyncio.sleep into a Rust Future
9+
pyo3_asyncio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
10+
})?;
11+
12+
println!("sleeping for 1s");
13+
fut.await?;
14+
println!("done");
15+
16+
Ok(())
17+
}

pyo3-asyncio-macros/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "pyo3-asyncio-macros"
3+
version = "0.13.0"
4+
authors = ["Andrew J Westlake <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
proc-macro = true
9+
10+
[dependencies]
11+
proc-macro2 = "1.0"
12+
quote = "1"
13+
syn = { version = "1", features = ["full"] }

0 commit comments

Comments
 (0)