Timeout a python function in windows -


I am trying to implement the timeout for a particular function. I have examined many questions in the SE and I have not found any problem, which matches my problem, because

  1. I'm running a dragon in Windows
  2. Timeout applies to one Python function on which I do not have control, i.e., it is defined in the pre-designed module.
  3. Python function is not a subprocess

    I am doing a pre-made custom module (MyModule) that is developed for specific tasks, and defined in it Functions are a function (MyFunk) due to external factors, it always has the tendency to walk, and I do not want to have the script hanging script.

    I am planning to add a timeout feature, as stated, after the execution of MyModule set_timeout (T) MyResult = MyModule.MyFunc () #MyFunc () or after T sec (Priority But the latter) After the expiration, go to this part of the script. I happened: The print 'myfun not fully executed' Second: print 'myfang full'

    But I'm not sure Which module to use on the dragon to get it Can be obtained Note that I based on a beginner I, and I wrote scripts directly SE North or Python documentation. I think that a good way to reach it is to create a decorator and thread.Join (timeout).

    Using the method. Keep in mind that there is no better way to kill the thread, so it is going on in your background, as much as your program is running.

    First, create a decorator like this: Threading Import Thread by Import Function: Define Timeout (Timeout)

      Def Deco (throw): @Funkastol WAFT (Function) DEF COVER (* Args, ** quarges): race = [exception ('function [% s] timeout exceeded [% s seconds]!'% (Func .__ name__, timeout)) def newFunc (): Try: [0] = func (* args, ** kwargs) Except the exception, e: reserve [0] = ett = thread (target = newfunk) t.eden = true tip: t. Except T.Join (timeout) exception, J: print 'error starting thread' raise je ret = res [0] ifinstance (ret, BaseException): Return Return Return Cover Cover Deco   

    Then, do something like this:

      try func = timeout (timeout = 16) (MyModule.MyFunc): except func (): pass #handle errors here  < / Pre> 

    You can use this decorator with anything you need:

     

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -