Attribute Macro RustPyNet::run_with_py
source · #[run_with_py]Expand description
The run_with_py procedural macro facilitates the execution of a given function within a Python context.
It dynamically creates a struct and its implementation based on the provided function. The function is then executed within a Python context, and its results are passed back through a channel.
Usage
#[run_with_py]
fn your_function_name(dict: &HashMap<String, String>) -> YourReturnType {
// Your function implementation here
}A more practical exemple:
#[run_with_py]
fn compute_sum(
dict: &HashMap<String, pyo3::types::PyAny>,
) -> Result<PythonTaskResult, PythonTaskError> {
// Sample Python code: compute the sum of 1 + 2
let sum: i32 = py.eval("1 + 2", None, None)?.extract()?;
Ok(PythonTaskResult::Int(sum))
}This macro will create the necessary infrastructure for the function to be run in a Python context.
Parameters
dict: AHashMapcontaining data that you wish to pass to the Python context.
Returns
Returns whatever your function is intended to return, wrapped in the necessary channel and context management code.
Errors
If there are any issues with obtaining the Python context or executing the function, an error will be returned.
The run_with_py procedural macro facilitates the execution of a given function within a Python context.
It dynamically creates a struct and its implementation based on the provided function. The function is then executed within a Python context, and its results are passed back through a channel.
Usage
#[run_with_py]
fn your_function_name(context: &PythonTaskContext) -> YourReturnType {
// Your function implementation here
}This macro will create the necessary infrastructure for the function to be run in a Python context.
Parameters
dict: AHashMapcontaining data that you wish to pass to the Python context.
Returns
Returns whatever your function is intended to return, wrapped in the necessary channel and context management code.
Errors
If there are any issues with obtaining the Python context or executing the function, an error will be returned.