2

I am attempting to run a program I have written for 1 second. If 1 second goes by then I want to switch to a new process and STOP the other one immediately. The following is my attempt

            p_create_time=time.time()
            p = multiprocessing.Process(target=queen4(number, numberblocked))
            p.start()
            p.join()
            i=0
            while i==0:
                if abs(time.time() - p_create_time) >= TIMEOUT:
                    p.terminate()
                    print "Stoped queens4.lp starting queens11.lp"
                    i=1
                    continue
                if not p.is_alive():
                    i=2
            print str(i)
            t=abs(time.time()-p_create_time)
            if i==1:
                d = multiprocessing.Process(target=queen11(number, numberblocked,t))
                d.start()

Instead of doing what I want if this goes over TIMEOUT then this script finishes p then runs dbut if it does not timeout then it works correctly. So my two questions are why is this not working (I am new to python) and ideas on how to fix it.

Thanks

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
Vladimir_314159
  • 1,457
  • 1
  • 9
  • 21
  • `p.join()`, with no parameter, *waits for the process to terminate*. The bulk of your code could be eliminated just by using `p.join(TIMEOUT)`, to have Python do the timeout for you (rather than the horribly inefficient busy-loop you now have). – jasonharper Apr 25 '18 at 12:33
  • will p.join(TIMEOUT) completly kill the process after TIMEOUT seconds? – Vladimir_314159 Apr 25 '18 at 12:40
  • infact this is definately not what I want see https://stackoverflow.com/questions/24855335/understanding-thread-jointimeout – Vladimir_314159 Apr 25 '18 at 13:39

0 Answers0