JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
776 stars 63 forks source link

PythonCall - Not all in-scope variables available with callback #312

Closed bryaan closed 1 year ago

bryaan commented 1 year ago

Affects: PythonCall

I don't have the ability to post the exact code that causes this to break. However it is a very strange issue that seems to be due to this library. I am using julia 1.9.0.

In the following code for some reason not all variables will be available in the callback. Changing the order of declaration doesn't seem to affect it. Nor does making the variable a global. The issue happens consistently and I'm not sure what can be done to debug.

pylib = pyimport("mypylib")

a, b = create()
x = Dates.Second(15)
nextGuiUpdateDateTime = 1
c = 5

function callback(p1, p2)
    @show a, x, c
    @show nextGuiUpdateDateTime # doesnt work
end
pylib.start_loop(callback)
cjdoris commented 1 year ago

Sounds weird! I can't think of anything to suggest other than reduce it to a MWE.

bryaan commented 1 year ago

I still have the code, may try that. But I worked around it by making the main program loop in julia. Now it occurs to me having the main loop in python may be an anti-pattern for this lib. It also has the disadvantage of making stack traces very long and deep in the pythoncall chain.

bryaan commented 1 year ago

Yea I figured it out, was simply not putting global in front of a variable. Strange how the bug presented itself though. Maybe the julia devs will want to improve the warning system for that.