Answers
- We initially must get Python from the GIL. We then must build a
PyDict
struct in order to store and pass Python variables between Python executions. We then define the Python code as a string literal and pass this into ourpy.eval
function with ourPyDict
storage. - We must make sure that we get Python from the GIL. We then use this to run the
py.eval
function with the import line of code passed in as a string literal. We must remember to pass in ourPyDict
storage to ensure that we can reference the module in the future. - We must remember that Python code returns a
PyAny
struct, which we can extract using the following code:let code = "5 + 6"; let result = py.eval(code, None, Some(&locals)).unwrap(); let number = result.extract::<i32>().unwrap();
We can see that
number
should be11
. - This is because the Python versions must keep stopping to clean up variables with the garbage collection mechanism.
- It would be slightly slower. This is because...