Skip to content

Interrupting running Python code from another thread #766

Closed

Description

@jduncalf

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.

Thanks.

Activity

  1. filmor commented on Nov 6, 2018

    @filmor
    Member

    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:

    class Interrupter
    {
        public Interrupter()
        {
            _interruptAction = DoInterrupt;
        }
    
        int DoInterrupt(IntPtr arg)
        {
            Exceptions.SetError(Exceptions.KeyboardInterrupt, "interrupted");
        }
    
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        delegate int PendingCall(IntPtr arg);
        readonly PendingCall _interruptAction;
    
        void Interrupt()
        {
            IntPtr func = Marshal.GetFunctionPointerForDelegate(_interruptAction);
            Runtime.Py_AddPendingCall(func, IntPtr.Zero);
        }
    }

    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 :)

  2. jduncalf commented on Nov 6, 2018

    @jduncalf
    Author

    Many thanks, i'll do that.

  3. jduncalf commented on Nov 7, 2018

    @jduncalf
    Author

    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?

  4. jduncalf commented on Nov 7, 2018

    @jduncalf
    Author

    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.

    Thanks again.

  5. changed the title [-]Intentional infinite loop[/-] [+]Interrupting running Python code from another thread[/+] on Nov 7, 2018
  6. filmor commented on Nov 7, 2018

    @filmor
    Member

    Nice, I'll see whether I can prepare a PR that includes this.

  7. added this to the 3.0.0 milestone on Nov 15, 2018
  8. amos402 commented on Nov 23, 2018

    @amos402
    Member

    @jduncalf I think this extension should be help https://github.com/amos402/py-xtimeout

  9. den-run-ai commented on Jan 17, 2019

    @den-run-ai
    Contributor

    anyone would like to submit a PR?

  10. amos402 commented on Jan 17, 2019

    @amos402
    Member

    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

  11. heartacker commented on Aug 14, 2020

    @heartacker

    @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

  12. kerol2r20 commented on Oct 5, 2020

    @kerol2r20

    I also meet this situation. Is there any idea to stop running script which embedded in C#?
    thanks

  13. TheCollegedude commented on Nov 17, 2020

    @TheCollegedude

    I also meet this situation. Is there any idea to stop running script which embedded in C#?
    thanks

    it seems we have to wait for 3.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions