Topics

Set a timeout for a python script


邓 君卓
 

I am now trying to run python codes from user input in a .netcore project on Linux(Debian). However, something like a dead loop may use up too much resource. I wonder if I can abort  oprations that cost too much time.

 

Maybe to set a timeout, so something like

while True:

    print("a")

won't block everything

 

This is my code at present:

 

py = Python.CreateEngine();

scope = py.CreateScope();

py.CreateScriptSourceFromString(code).Execute<object>(scope);

 

I want to set a timeout or kill a script when it is running for too long. (Maybe I could use a timer and kill the script when it runs for too long as a workaround.)


邓 君卓
 

I am now trying to run python codes from user input in a .netcore project on Linux(Debian). However, something like a dead loop may use up too much resource. I wonder if I can abort  oprations that cost too much time.

 

Maybe to set a timeout, so something like

while True:

    print("a")

won't block everything

 

This is my code at present:

 

py = Python.CreateEngine();

scope = py.CreateScope();

py.CreateScriptSourceFromString(code).Execute<object>(scope);

 

I want to set a timeout or kill a script when it is running for too long. (Maybe I could use a timer and kill the script when it runs for too long as a workaround.)