I want to implement a program that will terminate after running for some time t
, and the t
is read from command line using ArgumentParser
. Currently I 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 terminates after running for 3 seconds). But as I mentioned before, the running time (3.0 in the code snippet above) should be input from command line (eg. args.arg3 = 3.0) instead of hard coded. Apparently I cannot put time.sleep(args.arg3)
directly. I was wondering if there is any approach that could solve my problem? Answers without using daemon thread are also welcome! Thanks.
PS. If I put the argument parsing code outside of run
function like:
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 terminate after args.arg3
seconds and I'm confused about the reason. I would also be very appreciative if any one could explain the magic behind all of these... Thanks a lot!