4

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!

yvetterowe
  • 1,239
  • 7
  • 20
  • 34

1 Answers1

1

In your second example, when creating a Thread you should only pass the function, and the args should come as a second argument. like this:

Thread(target=run, args = (args.arg1, args.arg2))

So in your 2nd example you actually evaluate the function before creating a thread and returning None from the run function to the Thread class.

Also, according to the docs, when specifying daemon = True:

The significance of this flag is that the entire Python program exits when only daemon threads are left

This should work:

def run(arg1,arg2):
    class_instance = MultiThreadClass(arg1, arg2)
    class_instance.run()

if __name__ == '__main__':
    parser = create_arg_parser()
    args = parser.parse_args()
    run_thread = Thread(target=run, args=(args.arg1, args.arg2))
    run_thread.start()
    time.sleep(args.arg3)

This post will help you get start with Threads, furthermore i would read the docs to better understand the "magic".

Community
  • 1
  • 1
Udy
  • 2,492
  • 4
  • 23
  • 33