You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
can not make calls to Python on a separate thread #511
The following (copies from Example) worked fine
using (Py.GIL())
{
dynamic test = Py.Import("programs.test_program");
Console.WriteLine(test.func(args));
}
However, the below ends up hanging forever even though it's the same one line program as above. Without the ability to run on a separate thread how can I kill an execution if it takes too long. We deal with massive amount of python scripts at runtime it's gonna be a pain to modify them to terminated the costly computation.
using (Py.GIL())
{
dynamic test = Py.Import("programs.test_program");
Thread t = new Thread(delegate () {
Console.WriteLine(test.func(args));
});
}
Thanks for the pointers. Does this mean I have to get the source (after the fix) and then build it myself is there a binary I can just directly embed in my .Net application.
This is my code running the python script above and it hangs every time.
using (Py.GIL())
{
dynamic testModule = Py.Import("programs.test_program");
output = testModule.testWrapper();
}
Pythonnet installed using following wheel:
pythonnet-2.3.0-cp36-cp36m-win_amd64.whl
Python:3.6 64-bit
Would you please let me know if you can run this code
@keowang ok, i did some testing with pythonnet 2.3.0, but I still don't see your exact issue here. Show me the problem again. Here is my testing:
//#r "C:\Python\Python35_64b\Lib\site-packages\Python.Runtime.dll"usingPython.Runtime;usingSystem;namespaceinterrupt{classProgram{staticvoidMain(string[]args){dynamicintr;dynamicsys;PythonEngine.Initialize(true);//, true); second argument if for @rmadsen-ks branchusing(Py.GIL()){sys=Py.Import("sys");}sys.path.append(System.AppDomain.CurrentDomain.BaseDirectory);using(Py.GIL()){intr=Py.Import("interrupt_main");}using(Py.GIL()){try{intr.testWrapper();}catch{Console.WriteLine("some exception here");}}}}}
@denfromufa I appreciate your response. The issues is you are running the binary and surprisingly it worked. However, in Visual Studio debug mode intr.testWrapper() will hang every time. There is no way we can get around this by skipping the debug mode and directly monitoring the program behavior via logs which is hugely inconvenient. Would you mind looking into that see if there is some solution that we can have the consistent behavior in both debug and release mode. Thanks
Activity
den-run-ai commentedon Jul 14, 2017
what happens if thread is launched outside of Py.GIL() block?
keowang commentedon Jul 14, 2017
This is what happens:
An unhandled exception of type 'System.AccessViolationException' occurred in Python.Runtime.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
keowang commentedon Jul 14, 2017
Try to do something inside the python script by following https://stackoverflow.com/questions/492519/timeout-on-a-function-call. It's running fine in python but once again pythonnet hangs forever. I wonder what is the solution.
keowang commentedon Jul 14, 2017
Hi can I get an update, It's a blocker on our end. I would appreciate if you can advice. Thanks a lot!
den-run-ai commentedon Jul 14, 2017
The reason why interrupt does not work in pythonnet is due to this issue and corresponding WIP pull request:
#449
#450
den-run-ai commentedon Jul 14, 2017
The code in pull request is actually OK, but the tests for it are not good.
keowang commentedon Jul 14, 2017
so what's the fix?
den-run-ai commentedon Jul 14, 2017
The fix is in the pull request, just look at the diff:
https://github.com/pythonnet/pythonnet/pull/450/files
In short pythonnet should call Py_InitializeEx() instead of Py_Initialize().
keowang commentedon Jul 14, 2017
Thanks for the pointers. Does this mean I have to get the source (after the fix) and then build it myself is there a binary I can just directly embed in my .Net application.
keowang commentedon Jul 14, 2017
Or if this is easier. Would you mind advising me how to create a infinity loop detection when I am calling python code.
den-run-ai commentedon Jul 15, 2017
@keowang I tried with master branch and I don't see any issue:
den-run-ai commentedon Jul 15, 2017
@rmadsen-ks do you see any issue?
keowang commentedon Jul 15, 2017
@denfromufa
This is my code running the python script above and it hangs every time.
using (Py.GIL())
{
dynamic testModule = Py.Import("programs.test_program");
output = testModule.testWrapper();
}
Pythonnet installed using following wheel:
pythonnet-2.3.0-cp36-cp36m-win_amd64.whl
Python:3.6 64-bit
Would you please let me know if you can run this code
den-run-ai commentedon Jul 16, 2017
@keowang ok, i did some testing with pythonnet 2.3.0, but I still don't see your exact issue here. Show me the problem again. Here is my testing:
Then if I call from python:
If I call from C# compiled app:
keowang commentedon Jul 16, 2017
@denfromufa I appreciate your response. The issues is you are running the binary and surprisingly it worked. However, in Visual Studio debug mode
intr.testWrapper()will hang every time. There is no way we can get around this by skipping the debug mode and directly monitoring the program behavior via logs which is hugely inconvenient. Would you mind looking into that see if there is some solution that we can have the consistent behavior in both debug and release mode. Thanks10 remaining items
keowang commentedon Jul 19, 2017
keowang commentedon Jul 19, 2017
This is how it hangs
keowang commentedon Jul 19, 2017
Can I please ask what's your exception setting? Thanks!
den-run-ai commentedon Jul 19, 2017
you can see my exception settings in my screenshot
keowang commentedon Jul 19, 2017
yes I tried and it's the same thing
den-run-ai commentedon Jul 19, 2017
did you try break all: ctrl+alt+break
keowang commentedon Jul 19, 2017
press the key manually?
den-run-ai commentedon Jul 19, 2017
no, break in the debugger
keowang commentedon Jul 19, 2017
how do I do that?
den-run-ai commentedon Jul 19, 2017
debug -> break all
keowang commentedon Jul 19, 2017
keowang commentedon Jul 19, 2017
I do not see it here
keowang commentedon Jul 19, 2017
Hey I fixed it by enabling the native code which makes the debugging very slow. Can I do it without the native code?
den-run-ai commentedon Jul 19, 2017
"break all" has to be triggered during debugging. i do not have native code debugger enabled. please contact VS team @ msft about debugger issues.