0

t2.start, (the last line), does not execute until I press CTRL+C. Why is this the case?

import threading
from time import sleep
def thread1():
    try:
        while True:
            print("Thread1 run")
            sleep(3)
    except:
        print("thread 1 ended")
        ## Insert some clean up functions
        exit
def thread2():
  try:
      while True:
          print("thread2 running")
          sleep(3)
  except:
      ## Insert some cleanup functions
      exit

 t2 = threading.Thread(target = thread1())
 t1 = threading.Thread(target = thread2())

 t1.start()
 t2.start()
1
  • 3
    Not Pi specific. This is a general Python question.
    – joan
    Jun 13 '18 at 14:36
7

Neither thread is actually started.

Instead of

threading.Thread(target = thread1())

you should use

threading.Thread(target = thread1)

At the moment you are calling a function which never returns so subsequent lines of the script are not executed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.