python terminate program after specific time

Solutions on MaxInterview for python terminate program after specific time by the best coders in the world

showing results for - "python terminate program after specific time"
Fatma
08 Apr 2020
1import multiprocessing
2import time
3
4# Your foo function
5def foo(n):
6    for i in range(10000 * n):
7        print "Tick"
8        time.sleep(1)
9
10if __name__ == '__main__':
11    # Start foo as a process
12    p = multiprocessing.Process(target=foo, name="Foo", args=(10,))
13    p.start()
14
15    # Wait 10 seconds for foo
16    time.sleep(10)
17
18    # Terminate foo
19    p.terminate()
20
21    # Cleanup
22    p.join()
23