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
Interrupting running Python code from another thread #766
Hi, does anyone know the best way to stop a currently running PyScope.Exec() call. My C# app is multi-threaded and allows users to run scripts, however where there's an infinite loop, new Python calls can be executed but I can't stop the original infinite loop.
We currently do not expose Py_AddPendingCall, though, but assuming we did (there is PR #692 that uses it), you could probably translate the given example like this:
All Runtime functions are internal, so you will have to work within pythonnet's codebase itself. A PR adding an Interrupt() function on PythonEngine and PyScope would be highly appreciated :)
Hi, unfortunately I can't get this to work. I basically used the Interrupter class you posted above, but the DoInterrupt() callback never gets invoked.
Hi filmor, apologies, got it working. Had to change my C# host apps threading model. As a COM/DCOM MTA server the .exe primary thread was not the thread which eventually called PyEval_InitThreads, which is required for the Py_AddPendingCall supplied callback to work.
Activity
filmor commentedon Nov 6, 2018
See here https://stackoverflow.com/questions/1420957/stopping-embedded-python.
We currently do not expose
Py_AddPendingCall, though, but assuming we did (there is PR #692 that uses it), you could probably translate the given example like this:All
Runtimefunctions areinternal, so you will have to work withinpythonnet's codebase itself. A PR adding anInterrupt()function onPythonEngineandPyScopewould be highly appreciated :)jduncalf commentedon Nov 6, 2018
Many thanks, i'll do that.
jduncalf commentedon Nov 7, 2018
Hi, unfortunately I can't get this to work. I basically used the Interrupter class you posted above, but the DoInterrupt() callback never gets invoked.
Any ideas?
jduncalf commentedon Nov 7, 2018
Hi filmor, apologies, got it working. Had to change my C# host apps threading model. As a COM/DCOM MTA server the .exe primary thread was not the thread which eventually called
PyEval_InitThreads, which is required for thePy_AddPendingCallsupplied callback to work.Thanks again.
[-]Intentional infinite loop[/-][+]Interrupting running Python code from another thread[/+]filmor commentedon Nov 7, 2018
Nice, I'll see whether I can prepare a PR that includes this.
amos402 commentedon Nov 23, 2018
@jduncalf I think this extension should be help https://github.com/amos402/py-xtimeout
den-run-ai commentedon Jan 17, 2019
anyone would like to submit a PR?
amos402 commentedon Jan 17, 2019
I think this is not a problem that relate to pythonnet, the extension I supplied above can handle this case.
Or you can reference here to make a trace callback when you try to interrupt the work thread.
https://github.com/amos402/py-xtimeout/blob/3f17f5ed050045cd8dc13987ce2abb0187501f88/xtimeout/_xtimeout.cpp#L358-L379
https://github.com/amos402/py-xtimeout/blob/3f17f5ed050045cd8dc13987ce2abb0187501f88/xtimeout/_xtimeout.cpp#L86-L95
heartacker commentedon Aug 14, 2020
@heartacker i have the same requirement of stopping runing code embbed in wpf. if there is any easy way to stop them. tell me thanks
kerol2r20 commentedon Oct 5, 2020
I also meet this situation. Is there any idea to stop running script which embedded in C#?
thanks
TheCollegedude commentedon Nov 17, 2020
it seems we have to wait for 3.0.0