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()

closed as off-topic by joan, Steve Robillard, Aurora0001, Darth Vader Jun 13 '18 at 19:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – joan, Steve Robillard, Darth Vader
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Not Pi specific. This is a general Python question. – joan Jun 13 '18 at 14:36
5

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.