asyncio module in embedded mode issue #4699
-
Hi all Here is my development environment.
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.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments · 4 replies
-
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 |
Beta Was this translation helpful? Give feedback.
-
Hi @youknowone 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:
I would like to fully embed RustPython in my app. That is the reason I would like to keep freeze-stdlib feature. |
Beta Was this translation helpful? Give feedback.
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:
The code: