-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interrupt execution of a script #64
Comments
Yo can start script execution on a separate thread, then of course it is. |
It's on a separate thread than the main, but as much as I saw there's no built in way to provide a cancellation token to the |
Maybe we want to add an ExecuteAsync, or BeginExecute or something. On Thu, Jul 21, 2016 at 5:19 AM Hristo Hristov [email protected]
|
Passing a token to it wouldn't really accomplish much - you still need to inspect the token from the script code and abort (CancellationToken is a cooperative yielding mechanism, not pre-emptive). I would just publish the cancellation token into your script's context dictionary and allow scripts to call |
One option would be to have an easy way to inject a KeyboardInterrupt (i.e. On Thu, Jul 21, 2016 at 9:42 AM, Keith Rome [email protected]
|
I am not sure whether you want to cancel the running script from within the script or from the hosting environment. On the other hand, why do you want to use CancellationToken and not calling Thread.Abort() instead and handle ThreadAbortException as you need ? Option 1, cancelling from script : you could add the Thread.CurrentThread object of the thread called Execute() to the script context as a variable. Then from python the script call its Abort() method. Something like this : try
{
//...
ScriptScope execScope = PyEngine.CreateScope();
ScriptScope .SetVariable("ExecThread", Thread.CurentThread);
PyEngine.Execute(execScope);
//...
}
catch (ThreadAbortException Ex)
{
// cleanup code
} And from Python script : ExecThread.Abort() Option 2, from hosting application : simply call Abort() method of the thread called Execute() |
|
Yeah, I know it's a nasty way but I do not have any better idea, and for simple scripts it works. |
I want to resurrect the discussion here...I don't want to deal with PS.My scripts aren't simple..they consist of around 1k lines of code each so you can imagine how tedious it would be to check on every occasion if the task has been cancelled or not.. Edit 2 : @lafrank you could pass the cancellation token , the problem is that you still need to check it everytime you do something in your script Edit 3 : I'm really expecting to pass my cancellation token to the execute method of the Edit 4 : You could subscribe to C# object event and sys.exit() when the event is invoked :) |
I would love to see this feature added. I have an application that uses IronPython to execute user provided scripts so graceful exit hooks within the script are not an option. I want to add a "Stop" button to terminate the execution of any script at an arbitrary point and |
You could always use |
@KeithJRome I just gave the
To confirm, I attempted to interrupt the following two scripts using a handler via import time
time.sleep(3)
print('done') and import time
def sleep():
time.sleep(3)
def p():
print('done')
sleep()
p() I was not able to prevent the print statement in the first script but was able to interrupt the second one. I think |
@simo9000 try using the 'line' event. That's the event I've used before to suspend running code on "breakpoints" in a custom debugger IDE. |
@KeithJRome thanks for pushing me to understand |
So can this be closed out, or do you want another solution? |
I'm good with the However, it was @juno-craft that asked for the feature originally... |
it would be cool to get an example on how to do that what you did @simo9000 |
I'd also appreciate seeing an example of how to stop the script gracefully
from the traceback handler.
I write a .Net app in c# for hosting Python scripts and do use a traceback
handler for debugging it, but I have no idea how to stop the script from
within the handler. Well, except Thread.Abort() :-)
…On Thu, Dec 22, 2016 at 10:47 AM, juno-craft ***@***.***> wrote:
it would be cool if to get an example on how to do that what you used
@simo9000 <https://github.com/simo9000>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#64 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AO-XZLyTV8v_tEjV2jhpfANC2gdn57M9ks5rKkcigaJpZM4IgR0T>
.
|
Is there a way to use a CancellationToken to interrupt the execution of a script?
The text was updated successfully, but these errors were encountered: