0

I have a regression test script written in python that calls a number of test scripts also written in python and executes them. Every test script returns either pass or fail. I want to make sure that the regression test script doesn't wait forever for a return value and stop the running test script after sometime. I came across this answer and it works perfectly well. Except for a small caveat. The p.join() will not return a value from the test script which is essential. I can not use message passing, or a global variable since it is a huge code base used across multiple teams. Can someone please suggest something? I have looked at the multiprocessing documentation but I can not seem to find a clear cut answer.

The code runs on Windows machine and python version is 2.7x.

Community
  • 1
  • 1
user1343318
  • 2,093
  • 6
  • 32
  • 59

1 Answers1

1

You could use a Pool instead, which will return the result of the worker function directly:

import multiprocessing

def f():
  return "hi"

if __name__ == "__main__":
    pool = multiprocessing.Pool(1) # Just a one-process Pool.
    result = pool.apply_async(f)
    try:
        print(result.get(10)) # Wait 10 seconds
    except multiprocessing.TimeoutError:
        print("Timed out")
        pool.terminate() # Kill all (1, in this case) processes in the pool
        pool.join()
dano
  • 91,354
  • 19
  • 222
  • 219