Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

asyncio module in embedded mode issue #4699

Answered by erhanbaris
erhanbaris asked this question in Q&A
Discussion options

Hi all
I would like to extend my application with python and RustPython is quite a nice library to do that. But, I had an issue when I try to use asyncio module. I hope some already know how to fix this issue.

Here is my development environment.

  • rustc: rustc 1.67.1 (d5a82bbd2 2023-02-07)
  • windows 10
  • Revision from repo: 2efc566

Also, reproduction code:

use rustpython_vm as vm;
use rustpython_vm::VirtualMachine;

fn init_vm(vm: &mut VirtualMachine) {
    vm.add_frozen(rustpython_pylib::FROZEN_STDLIB);
}

fn main() -> vm::PyResult<()> {
    vm::Interpreter::with_init(Default::default(), init_vm).enter(|vm| {
        let scope = vm.new_scope_with_builtins();

        let code_obj = vm
            .compile(
                r#"
import asyncio                
print("Hello World!")
"#,
                vm::compiler::Mode::Exec,
                "<embedded>".to_owned(),
            )
            .map_err(|err| vm.new_syntax_error(&err))?;

        if let Err(error) = vm.run_code_obj(code_obj, scope) {
            vm.print_exception(error);
        }

        Ok(())
    })
}

Execution output.

Traceback (most recent call last):
  File "<embedded>", line 2, in <module>
  File "asyncio", line 6, in <module>
  File "selectors", line 11, in <module>
ModuleNotFoundError: No module named 'math'
You must be logged in to vote

Hi @youknowone
Thank you for your fast response. Actually, I could not find example for my case and I found some information from the repo and codes. But I tried to use call_between_rust_and_python.rs as a reference and got another issue.

Cargo.toml:

rustpython = {git = "http://github.com/rustpython/rustpython.git", features = ["stdlib", "freeze-stdlib"]}

The code:

use rustpython::vm::{VirtualMachine, PyResult, stdlib::get_module_inits, compiler};

fn main() -> PyResult<()> {
    let interp = rustpython::InterpreterConfig::new()
    .init_stdlib()
    .interpreter();


    interp.enter(|vm| {
        let scope = vm.new_scope_with_builtins();

        let code_obj = vm
            .compile(…

Replies: 2 comments · 4 replies

Comment options

Hi, would you tell me which example did you use as reference? I'd like to update outdated documents or examples.

I recommend to use rustpython rather than rustpython_vm for casual usage
Check this example: https://github.com/RustPython/RustPython/blob/main/examples/call_between_rust_and_python.rs

You must be logged in to vote
0 replies
Comment options

Hi @youknowone
Thank you for your fast response. Actually, I could not find example for my case and I found some information from the repo and codes. But I tried to use call_between_rust_and_python.rs as a reference and got another issue.

Cargo.toml:

rustpython = {git = "http://github.com/rustpython/rustpython.git", features = ["stdlib", "freeze-stdlib"]}

The code:

use rustpython::vm::{VirtualMachine, PyResult, stdlib::get_module_inits, compiler};

fn main() -> PyResult<()> {
    let interp = rustpython::InterpreterConfig::new()
    .init_stdlib()
    .interpreter();


    interp.enter(|vm| {
        let scope = vm.new_scope_with_builtins();

        let code_obj = vm
            .compile(
                r#"
import asyncio
print("Hello World!")
"#,
                compiler::Mode::Exec,
                "<embedded>".to_owned(),
            )
            .map_err(|err| vm.new_syntax_error(&err))?;

        if let Err(error) = vm.run_code_obj(code_obj, scope) {
            vm.print_exception(error);
        }

        Ok(())
    })
}

The output is:

Running `target\debug\server.exe

thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\debug\server.exe` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)

I would like to fully embed RustPython in my app. That is the reason I would like to keep freeze-stdlib feature.

You must be logged in to vote
4 replies
@youknowone
Comment options

maybe release build will be ok.
For debug build, see this file for windows environment
https://github.com/RustPython/RustPython/blob/main/.cargo/config.toml

@erhanbaris
Comment options

Yes, release is working. I create onfig.toml with same configuration and now debug build works as well.
But, call_between_rust_and_python.rs not building.
Cargo.toml:

rustpython = {git = "http://github.com/rustpython/rustpython.git", features = ["stdlib", "freeze-stdlib"]}

Error message:

error[E0433]: failed to resolve: could not find `rustpython_vm` in the list of imported crates
  --> server\src\main.rs:31:1
   |
31 | #[pymodule]
   | ^^^^^^^^^^^ could not find `rustpython_vm` in the list of imported crates
   |
   = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared crate or module `rustpython_vm`
  --> server\src\main.rs:68:12
   |
68 |     struct RustStruct {
   |            ^^^^^^^^^^ use of undeclared crate or module `rustpython_vm`

error[E0433]: failed to resolve: could not find `rustpython_vm` in the list of imported crates
  --> server\src\main.rs:67:21
   |
67 |     #[derive(Debug, PyPayload)]
   |                     ^^^^^^^^^ could not find `rustpython_vm` in the list of imported crates
   |
   = note: this error originates in the derive macro `PyPayload` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared crate or module `rustpython_vm`
  --> server\src\main.rs:67:21
   |
67 |     #[derive(Debug, PyPayload)]
   |                     ^^^^^^^^^ use of undeclared crate or module `rustpython_vm`
   |
   = note: this error originates in the derive macro `PyPayload` (in Nightly builds, run with -Z macro-backtrace for more info)
help: there is a crate or module with a similar name
   |
67 |     #[derive(Debug, rustpython)]
   |                     ~~~~~~~~~~

error[E0433]: failed to resolve: could not find `rustpython_vm` in the list of imported crates
  --> server\src\main.rs:72:5
   |
72 |     #[pyclass]
   |     ^^^^^^^^^^ could not find `rustpython_vm` in the list of imported crates
   |
   = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: could not find `rustpython_vm` in the list of imported crates
  --> server\src\main.rs:75:12
   |
75 |         fn numbers(&self) -> NumVec {
   |            ^^^^^^^ could not find `rustpython_vm` in the list of imported crates

error[E0433]: failed to resolve: could not find `rustpython_vm` in the list of imported crates
  --> server\src\main.rs:31:1
   |
31 | #[pymodule]
   | ^^^^^^^^^^^ could not find `rustpython_vm` in the list of imported crates
   |
   = note: this error originates in the attribute macro `pymodule` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: could not find `rustpython_vm` in the list of imported crates
  --> server\src\main.rs:68:12
   |
68 |     struct RustStruct {
   |            ^^^^^^^^^^ could not find `rustpython_vm` in the list of imported crates

error[E0425]: cannot find value `CELL` in this scope
  --> server\src\main.rs:31:1
   |
31 | #[pymodule]
   | ^^^^^^^^^^^ not found in this scope
   |
   = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0425, E0433.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `server` due to 9 previous errors
@youknowone
Comment options

oh... it looks like rustpython_vm also needed to be added as dependency.

rustpython = {git = "http://github.com/rustpython/rustpython.git", features = ["stdlib", "freeze-stdlib"]}
rustpython_vm = {git = "http://github.com/rustpython/rustpython.git"}
@erhanbaris
Comment options

Thanks. That works well.

Answer selected by erhanbaris
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.