Python: Threading specific number of times
<p>I have the following code that uses <code>threading</code> and prints the current count.</p>

<pre><code>import threading

count = 0
def worker():
    """thread worker function"""
    global count
    count += 1
    print(count)

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()
</code></pre>

<p>It is currently set to 5 threads. How can I have it continue running the threads until it reaches a certain #. i.e. run at 5 threads until <code>worker()</code> has run 100 times.</p>

<p>just do a while loop, but protect your counter and your test with a lock, otherwise the value tested will be different from the one you just increased.</p>

<p>I have added the thread id so we see which thread is actually increasing the counter.</p>

<p>Also: check first, increase after.</p>

<p>And wait for the threads in the end.</p>

<pre><code>import threading

lck = threading.Lock()

count = 0
def worker():
    global count
    """thread worker function"""
    while True:
        lck.acquire()
        if count==100:
            lck.release()
            break
        count += 1
        print(threading.current_thread() ,count)
        lck.release()

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()

for t in threads:
    t.join()
</code></pre>

<p>result:</p>

<pre><code>(&lt;Thread(Thread-1, started 5868)&gt;, 1)
(&lt;Thread(Thread-2, started 7152)&gt;, 2)
(&lt;Thread(Thread-3, started 6348)&gt;, 3)
(&lt;Thread(Thread-4, started 6056)&gt;, 4)
(&lt;Thread(Thread-1, started 5868)&gt;, 5)
(&lt;Thread(Thread-5, started 5748)&gt;, 6)
(&lt;Thread(Thread-2, started 7152)&gt;, 7)
(&lt;Thread(Thread-3, started 6348)&gt;, 8)
(&lt;Thread(Thread-4, started 6056)&gt;, 9)
(&lt;Thread(Thread-1, started 5868)&gt;, 10)
(&lt;Thread(Thread-5, started 5748)&gt;, 11)
(&lt;Thread(Thread-2, started 7152)&gt;, 12)
(&lt;Thread(Thread-3, started 6348)&gt;, 13)
(&lt;Thread(Thread-4, started 6056)&gt;, 14)
(&lt;Thread(Thread-1, started 5868)&gt;, 15)
(&lt;Thread(Thread-5, started 5748)&gt;, 16)
(&lt;Thread(Thread-2, started 7152)&gt;, 17)
(&lt;Thread(Thread-3, started 6348)&gt;, 18)
(&lt;Thread(Thread-4, started 6056)&gt;, 19)
(&lt;Thread(Thread-1, started 5868)&gt;, 20)
(&lt;Thread(Thread-5, started 5748)&gt;, 21)
(&lt;Thread(Thread-2, started 7152)&gt;, 22)
(&lt;Thread(Thread-3, started 6348)&gt;, 23)
(&lt;Thread(Thread-4, started 6056)&gt;, 24)
(&lt;Thread(Thread-1, started 5868)&gt;, 25)
(&lt;Thread(Thread-5, started 5748)&gt;, 26)
(&lt;Thread(Thread-2, started 7152)&gt;, 27)
(&lt;Thread(Thread-3, started 6348)&gt;, 28)
(&lt;Thread(Thread-4, started 6056)&gt;, 29)
(&lt;Thread(Thread-1, started 5868)&gt;, 30)
(&lt;Thread(Thread-5, started 5748)&gt;, 31)
(&lt;Thread(Thread-2, started 7152)&gt;, 32)
(&lt;Thread(Thread-3, started 6348)&gt;, 33)
(&lt;Thread(Thread-4, started 6056)&gt;, 34)
(&lt;Thread(Thread-1, started 5868)&gt;, 35)
(&lt;Thread(Thread-5, started 5748)&gt;, 36)
(&lt;Thread(Thread-2, started 7152)&gt;, 37)
(&lt;Thread(Thread-3, started 6348)&gt;, 38)
(&lt;Thread(Thread-4, started 6056)&gt;, 39)
(&lt;Thread(Thread-1, started 5868)&gt;, 40)
(&lt;Thread(Thread-5, started 5748)&gt;, 41)
(&lt;Thread(Thread-2, started 7152)&gt;, 42)
(&lt;Thread(Thread-3, started 6348)&gt;, 43)
(&lt;Thread(Thread-4, started 6056)&gt;, 44)
(&lt;Thread(Thread-1, started 5868)&gt;, 45)
(&lt;Thread(Thread-5, started 5748)&gt;, 46)
(&lt;Thread(Thread-2, started 7152)&gt;, 47)
(&lt;Thread(Thread-3, started 6348)&gt;, 48)
(&lt;Thread(Thread-4, started 6056)&gt;, 49)
(&lt;Thread(Thread-1, started 5868)&gt;, 50)
(&lt;Thread(Thread-5, started 5748)&gt;, 51)
(&lt;Thread(Thread-2, started 7152)&gt;, 52)
(&lt;Thread(Thread-3, started 6348)&gt;, 53)
(&lt;Thread(Thread-4, started 6056)&gt;, 54)
(&lt;Thread(Thread-1, started 5868)&gt;, 55)
(&lt;Thread(Thread-5, started 5748)&gt;, 56)
(&lt;Thread(Thread-2, started 7152)&gt;, 57)
(&lt;Thread(Thread-3, started 6348)&gt;, 58)
(&lt;Thread(Thread-4, started 6056)&gt;, 59)
(&lt;Thread(Thread-1, started 5868)&gt;, 60)
(&lt;Thread(Thread-5, started 5748)&gt;, 61)
(&lt;Thread(Thread-2, started 7152)&gt;, 62)
(&lt;Thread(Thread-3, started 6348)&gt;, 63)
(&lt;Thread(Thread-4, started 6056)&gt;, 64)
(&lt;Thread(Thread-1, started 5868)&gt;, 65)
(&lt;Thread(Thread-5, started 5748)&gt;, 66)
(&lt;Thread(Thread-2, started 7152)&gt;, 67)
(&lt;Thread(Thread-3, started 6348)&gt;, 68)
(&lt;Thread(Thread-4, started 6056)&gt;, 69)
(&lt;Thread(Thread-1, started 5868)&gt;, 70)
(&lt;Thread(Thread-5, started 5748)&gt;, 71)
(&lt;Thread(Thread-2, started 7152)&gt;, 72)
(&lt;Thread(Thread-3, started 6348)&gt;, 73)
(&lt;Thread(Thread-4, started 6056)&gt;, 74)
(&lt;Thread(Thread-1, started 5868)&gt;, 75)
(&lt;Thread(Thread-5, started 5748)&gt;, 76)
(&lt;Thread(Thread-2, started 7152)&gt;, 77)
(&lt;Thread(Thread-3, started 6348)&gt;, 78)
(&lt;Thread(Thread-4, started 6056)&gt;, 79)
(&lt;Thread(Thread-1, started 5868)&gt;, 80)
(&lt;Thread(Thread-5, started 5748)&gt;, 81)
(&lt;Thread(Thread-2, started 7152)&gt;, 82)
(&lt;Thread(Thread-3, started 6348)&gt;, 83)
(&lt;Thread(Thread-4, started 6056)&gt;, 84)
(&lt;Thread(Thread-1, started 5868)&gt;, 85)
(&lt;Thread(Thread-5, started 5748)&gt;, 86)
(&lt;Thread(Thread-2, started 7152)&gt;, 87)
(&lt;Thread(Thread-3, started 6348)&gt;, 88)
(&lt;Thread(Thread-4, started 6056)&gt;, 89)
(&lt;Thread(Thread-1, started 5868)&gt;, 90)
(&lt;Thread(Thread-5, started 5748)&gt;, 91)
(&lt;Thread(Thread-2, started 7152)&gt;, 92)
(&lt;Thread(Thread-3, started 6348)&gt;, 93)
(&lt;Thread(Thread-4, started 6056)&gt;, 94)
(&lt;Thread(Thread-1, started 5868)&gt;, 95)
(&lt;Thread(Thread-5, started 5748)&gt;, 96)
(&lt;Thread(Thread-2, started 7152)&gt;, 97)
(&lt;Thread(Thread-3, started 6348)&gt;, 98)
(&lt;Thread(Thread-4, started 6056)&gt;, 99)
(&lt;Thread(Thread-1, started 5868)&gt;, 100)
</code></pre>

python multithreading python-multithreading