Как отмечали другие, норма должна установить флаг остановки. Для чего-то легкого (без подкласса Thread, нет глобальной переменной), лямбда-обратный вызов является опцией. (Обратите внимание на круглые скобки в if stop()
.)
import threading
import time
def do_work(id, stop):
print("I am thread", id)
while True:
print("I am thread {} doing something".format(id))
if stop():
print(" Exiting loop.")
break
print("Thread {}, signing off".format(id))
def main():
stop_threads = False
workers = []
for id in range(0,3):
tmp = threading.Thread(target=do_work, args=(id, lambda: stop_threads))
workers.append(tmp)
tmp.start()
time.sleep(3)
print('main: done sleeping; time to stop the threads.')
stop_threads = True
for worker in workers:
worker.join()
print('Finis.')
if __name__ == '__main__':
main()
Замена print()
функцией pr()
, которая всегда сбрасывается (sys.stdout.flush()
), может улучшить точность вывода оболочки.
(проверяется только на Windows / Eclipse / Python3.3)