Threaded counters



examples/threads/mini_counter.py
import threading
import sys

class ThreadedCount(threading.Thread):
    def run(self):
        for cnt in range(6):
            print(f"{cnt} {threading.current_thread().name}")
        return

a = ThreadedCount()
b = ThreadedCount()
c = ThreadedCount()

a.start()
b.start()
c.start()
print('main - Running {} threads'.format(threading.active_count()))

a.join()
b.join()
c.join()
print("main - thread is done")

0 Thread-1
1 Thread-1
0 Thread-2
2 Thread-1
1 Thread-2
0 Thread-3
3 Thread-1
2 Thread-2
main - Running 4 threads
3 Thread-2
1 Thread-3
4 Thread-2
2 Thread-3
5 Thread-2
3 Thread-3
4 Thread-1
4 Thread-3
5 Thread-1
5 Thread-3
main - thread is done