0

I have a function that calls another function. I need everything to terminate after 5 seconds. The second function takes hours if let run to completion. How can I stop everything from running after 5 seconds without making changes to the second function? To clarify,

import datetime
def script1(arg):
    end_time = datetime.datetime.now() + datetime.timedelta(seconds=5)
    while True:
        if datetime.datetime.now() >= end_time:
            break
        else:
            os.system("python3 script2.py")

Currently I get stuck on the last line for the entirety of scrip2. I need it to terminate after 5 seconds.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Fin
  • 11
  • 2
  • 1
    You should probably use the `subprocess` module, instead of calling `os.system`. You can find the docs [here](https://docs.python.org/3/library/subprocess.html). You can pass a timeout to the `subprocess.call`. – Lysandros Nikolaou Mar 20 '18 at 10:26
  • If script2.py as it is written doesn't stop in a controlled way from outside influences, you have to kill it with a signal. – Arndt Jonasson Mar 20 '18 at 10:30

0 Answers0