GithubHelp home page GithubHelp logo

Comments (8)

DJSures avatar DJSures commented on July 28, 2024 1

You can create an exception type...

public class PythonStopException : Exception {
}

Use a cancellation token and source, or a volatile bool. You choose...

    volatile bool _cancelRequested = false;

Create a delegate for the Trace that checks the cancelation token or volatile bool

    private TracebackDelegate OnTraceback(TraceBackFrame frame, string result, object payload) {

      if (_cancelRequested)
        throw new PythonStopException();

      return OnTraceback;
    }

in your thread to launch the script, initialize the engine and set the trace to the ontraceback delegate...

      _engine = IronPython.Hosting.Python.CreateEngine();
      _engine.SetTrace(OnTraceback);

Make sure you catch the exception for the PythonStopException that you made, so it doesn't roll down as an error in your try/catch surrounding the Execute()

try {
   _engine.Execute(script)
} catch (PythonStopException) {
} 

from ironpython3.

slozier avatar slozier commented on July 28, 2024 1

Glad you found a solution.

Thanks for the suggestion; however, that is deprecated as well.

Just a side note, it's not really deprecated. It was marked as Obsolete right off the bat to discourage its use. See https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib0046

from ironpython3.

slozier avatar slozier commented on July 28, 2024

There's the System.Runtime.ControlledExecution which we might be usable instead of Thread.Abort although I haven't had a chance to try it out myself...

from ironpython3.

DJSures avatar DJSures commented on July 28, 2024

There's the System.Runtime.ControlledExecution which we might be usable instead of Thread.Abort although I haven't had a chance to try it out myself...

Thanks for the suggestion; however, that is deprecated as well. Do you have another suggestion? Isn't there a place in the IronPython library where we can insert a cancellationtoken to be checked during execution? I don't need it to be a Task or async. That's fine, i just need to somehow cancel the execution of a script from another thread.

image

from ironpython3.

Muny avatar Muny commented on July 28, 2024

FWIW, I check for cancellation inside a Traceback callback (scriptEngine.SetTrace()), and raise an exception if so. It probably has somewhat of a performance impact, and it won't immediately cancel things like sleep().

I do wish there was a neater solution.

from ironpython3.

wally96334 avatar wally96334 commented on July 28, 2024

I am trying to resolve the same problem.

#1779

My approach is to spin off an app domain, then trash the app domain in the event that execution is to be aborted.

from ironpython3.

DJSures avatar DJSures commented on July 28, 2024

FWIW, I check for cancellation inside a Traceback callback (scriptEngine.SetTrace()), and raise an exception if so. It probably has somewhat of a performance impact, and it won't immediately cancel things like sleep().

I do wish there was a neater solution.

That sounds like a good solution but i can't seem to find SetTrace() in the ScriptEngine. Do you have an example to share 🙏

*EDIT: nevermind, i was creating the runtime differently. I found it by creating the engine. Thanks! I'll go ahead and try this.

from ironpython3.

DJSures avatar DJSures commented on July 28, 2024

FWIW, I check for cancellation inside a Traceback callback (scriptEngine.SetTrace()), and raise an exception if so. It probably has somewhat of a performance impact, and it won't immediately cancel things like sleep().

I do wish there was a neater solution.

That works flawlessly!! Thank you so much. I was in a bit of a panic - and this is far more elegant than Thread.Abort().

from ironpython3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.