Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1320

Python • Re: Unknown threads running in Python application

$
0
0
Are you using something like this? How are you checking the running threads?

Code:

#!/usr/bin/env python3import threadingfrom time import sleep# global variable 'done' is the trigger to close threadsdone = False# T_running is a flag T_running = False# threaddef thread_function():   global done,T_running   T_running = True   print("Thread starting")   # thread loop   while not done:      print("Thread running")      sleep(1)   print("Thread finished")   T_running = False# start threadth = threading.Thread(target=thread_function)th.start()# main loop. Here is where the code runs until you want to close everything.while not done:   # this is blocking. input doesn't return until you type something or CTRL-C   try:      mychar = input("Q<ENTER> to quit\n")   except KeyboardInterrupt:      print("Stopping thread")      done = True      continue   if mychar == "q":      print("Stopping thread")      done = True# wait for threads to closewhile T_running:   print("waiting for thread finish")   sleep(0.5)print("All finished")

Statistics: Posted by SurferTim — Thu Jan 09, 2025 4:22 pm



Viewing all articles
Browse latest Browse all 1320

Trending Articles