December 2018
Beginner to intermediate
796 pages
19h 54m
English
As mentioned before, in general, stopping a thread is a bad idea, and the same goes for a process. Being sure you've taken care to dispose and close everything that is open can be quite difficult. However, there are situations in which you might want to be able to stop a thread, so let me show you how to do it:
# stop.pyimport threadingfrom time import sleepclass Fibo(threading.Thread): def __init__(self, *a, **kwa): super().__init__(*a, **kwa) self._running = True def stop(self): self._running = False def run(self): a, b = 0, 1 while self._running: print(a, end=' ') a, b = b, a + b sleep(0.07) print()fibo = Fibo()fibo.start()sleep(1)fibo.stop()fibo.join()print('All done.')
For this example, we use a Fibonacci ...
Read now
Unlock full access