indygreg / PyOxidizer

A modern Python application packaging and distribution tool
Mozilla Public License 2.0
5.51k stars 239 forks source link

pyembed asyncio #703

Closed Omicronlawful closed 1 year ago

Omicronlawful commented 1 year ago

Using async python isnt possible yet

include!("../../py/build/default_python_config.rs");

#[pyo3_asyncio::tokio::main]
async fn main()  -> PyResult<()>{
    // Get config from default_python_config.rs.
    let config = default_python_config();

    let interp = pyembed::MainPythonInterpreter::new(config).unwrap();

    // `py` is a `pyo3::Python` instance.
    let v = interp.with_gil(|py| {
        let asyncio = py.import("asyncio").unwrap();
       pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),)).unwrap())
        // convert asyncio.sleep into a Rust Future
    }).unwrap().await;
    Ok(())
}
error: future cannot be sent between threads safely
   --> src/main.rs:7:1
    |
7   | #[pyo3_asyncio::tokio::main]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `main` is not `Send`
    |
    = help: within `impl Future<Output = Result<(), PyErr>>`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
note: future is not `Send` as this value is used across an await
   --> src/main.rs:25:6
    |
16  |     let interp = pyembed::MainPythonInterpreter::new(config).unwrap();
    |         ------ has type `MainPythonInterpreter<'_, '_>` which is not `Send`
...
25  |     v.await;
    |      ^^^^^^ await occurs here, with `interp` maybe used later
26  |     Ok(())
27  | }
    | - `interp` is later dropped here
note: required by a bound in `pyo3_asyncio::tokio::run`
   --> /Users/frederik/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-asyncio-0.17.0/src/tokio.rs:238:39
    |
238 |     F: Future<Output = PyResult<T>> + Send + 'static,
    |                                       ^^^^ required by this bound in `run`
    = note: this error originates in the attribute macro `pyo3_asyncio::tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)

error: future cannot be sent between threads safely
   --> src/main.rs:7:1
    |
7   | #[pyo3_asyncio::tokio::main]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `main` is not `Send`
    |
    = help: within `impl Future<Output = Result<(), PyErr>>`, the trait `std::marker::Send` is not implemented for `*mut c_void`
note: future is not `Send` as this value is used across an await
   --> src/main.rs:25:6
    |
16  |     let interp = pyembed::MainPythonInterpreter::new(config).unwrap();
    |         ------ has type `MainPythonInterpreter<'_, '_>` which is not `Send`
...
25  |     v.await;
    |      ^^^^^^ await occurs here, with `interp` maybe used later
26  |     Ok(())
27  | }
    | - `interp` is later dropped here
note: required by a bound in `pyo3_asyncio::tokio::run`
   --> /Users/frederik/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-asyncio-0.17.0/src/tokio.rs:238:39
    |
238 |     F: Future<Output = PyResult<T>> + Send + 'static,
    |                                       ^^^^ required by this bound in `run`
    = note: this error originates in the attribute macro `pyo3_asyncio::tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)