I want to implement a program that will be completed after starting for some time t, and is tread from the command line using ArgumentParser. I currently have the following code (omit some details):
def run():
parser = create_arg_parser()
args = parser.parse_args()
class_instance = MultiThreadClass(args.arg1, args.arg2)
class_instance.run()
if __name__ == '__main__':
run_thread = Thread(target=run)
run_thread.daemon = True
run_thread.start()
time.sleep(3.0)
The program works as I expect (it ends after starting within 3 seconds). But, as I mentioned earlier, the runtime (3.0 in the code snippet above) should be entered from the command line (for example, args.arg3 = 3.0) instead of hard coding. Apparently, I can’t directly deliver time.sleep(args.arg3). I was wondering if there is any approach that could solve my problem? Answers without using a daemon are also welcome! Thank.
PS. If I put the argument analysis code outside the function run, for example:
def run(args):
class_instance = MultiThreadClass(args.arg1, args.arg2)
class_instance.run()
if __name__ == '__main__':
parser = create_arg_parser()
args = parser.parse_args()
run_thread = Thread(target=run(args))
run_thread.daemon = True
run_thread.start()
time.sleep(args.arg3)
The program will not end after args.arg3seconds, and I am confused for this reason. I would also be very grateful if anyone could explain the magic behind all of these ... Thank you very much!