while getattr(t, 'do_run', True):
    print('working on %s' % arg)
time.sleep(1)
print('Stopping as you wish.')

def main():
    t = threading.Thread(target=doit, args=('task',))
t.start()
time.sleep(5)
t.do_run = False
t.join()
if __name__ == '__main__':
    main()
Как остановить циклический поток в Python?