Python:特定于线程的次数
我有以下代码,它使用Python:特定于线程的次数,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,我有以下代码,它使用线程并打印当前计数 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
线程
并打印当前计数
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()
它当前设置为5个线程。我怎样才能让它继续运行线程,直到达到某个#。i、 e.以5个线程运行,直到
worker()
运行100次。只需执行while循环,但要用锁保护计数器和测试,否则测试的值将与刚才增加的值不同
我已经添加了线程id,所以我们可以看到哪个线程实际上在增加计数器
另外:先检查,后增加
然后等待最后的线程
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()
结果:
(<Thread(Thread-1, started 5868)>, 1)
(<Thread(Thread-2, started 7152)>, 2)
(<Thread(Thread-3, started 6348)>, 3)
(<Thread(Thread-4, started 6056)>, 4)
(<Thread(Thread-1, started 5868)>, 5)
(<Thread(Thread-5, started 5748)>, 6)
(<Thread(Thread-2, started 7152)>, 7)
(<Thread(Thread-3, started 6348)>, 8)
(<Thread(Thread-4, started 6056)>, 9)
(<Thread(Thread-1, started 5868)>, 10)
(<Thread(Thread-5, started 5748)>, 11)
(<Thread(Thread-2, started 7152)>, 12)
(<Thread(Thread-3, started 6348)>, 13)
(<Thread(Thread-4, started 6056)>, 14)
(<Thread(Thread-1, started 5868)>, 15)
(<Thread(Thread-5, started 5748)>, 16)
(<Thread(Thread-2, started 7152)>, 17)
(<Thread(Thread-3, started 6348)>, 18)
(<Thread(Thread-4, started 6056)>, 19)
(<Thread(Thread-1, started 5868)>, 20)
(<Thread(Thread-5, started 5748)>, 21)
(<Thread(Thread-2, started 7152)>, 22)
(<Thread(Thread-3, started 6348)>, 23)
(<Thread(Thread-4, started 6056)>, 24)
(<Thread(Thread-1, started 5868)>, 25)
(<Thread(Thread-5, started 5748)>, 26)
(<Thread(Thread-2, started 7152)>, 27)
(<Thread(Thread-3, started 6348)>, 28)
(<Thread(Thread-4, started 6056)>, 29)
(<Thread(Thread-1, started 5868)>, 30)
(<Thread(Thread-5, started 5748)>, 31)
(<Thread(Thread-2, started 7152)>, 32)
(<Thread(Thread-3, started 6348)>, 33)
(<Thread(Thread-4, started 6056)>, 34)
(<Thread(Thread-1, started 5868)>, 35)
(<Thread(Thread-5, started 5748)>, 36)
(<Thread(Thread-2, started 7152)>, 37)
(<Thread(Thread-3, started 6348)>, 38)
(<Thread(Thread-4, started 6056)>, 39)
(<Thread(Thread-1, started 5868)>, 40)
(<Thread(Thread-5, started 5748)>, 41)
(<Thread(Thread-2, started 7152)>, 42)
(<Thread(Thread-3, started 6348)>, 43)
(<Thread(Thread-4, started 6056)>, 44)
(<Thread(Thread-1, started 5868)>, 45)
(<Thread(Thread-5, started 5748)>, 46)
(<Thread(Thread-2, started 7152)>, 47)
(<Thread(Thread-3, started 6348)>, 48)
(<Thread(Thread-4, started 6056)>, 49)
(<Thread(Thread-1, started 5868)>, 50)
(<Thread(Thread-5, started 5748)>, 51)
(<Thread(Thread-2, started 7152)>, 52)
(<Thread(Thread-3, started 6348)>, 53)
(<Thread(Thread-4, started 6056)>, 54)
(<Thread(Thread-1, started 5868)>, 55)
(<Thread(Thread-5, started 5748)>, 56)
(<Thread(Thread-2, started 7152)>, 57)
(<Thread(Thread-3, started 6348)>, 58)
(<Thread(Thread-4, started 6056)>, 59)
(<Thread(Thread-1, started 5868)>, 60)
(<Thread(Thread-5, started 5748)>, 61)
(<Thread(Thread-2, started 7152)>, 62)
(<Thread(Thread-3, started 6348)>, 63)
(<Thread(Thread-4, started 6056)>, 64)
(<Thread(Thread-1, started 5868)>, 65)
(<Thread(Thread-5, started 5748)>, 66)
(<Thread(Thread-2, started 7152)>, 67)
(<Thread(Thread-3, started 6348)>, 68)
(<Thread(Thread-4, started 6056)>, 69)
(<Thread(Thread-1, started 5868)>, 70)
(<Thread(Thread-5, started 5748)>, 71)
(<Thread(Thread-2, started 7152)>, 72)
(<Thread(Thread-3, started 6348)>, 73)
(<Thread(Thread-4, started 6056)>, 74)
(<Thread(Thread-1, started 5868)>, 75)
(<Thread(Thread-5, started 5748)>, 76)
(<Thread(Thread-2, started 7152)>, 77)
(<Thread(Thread-3, started 6348)>, 78)
(<Thread(Thread-4, started 6056)>, 79)
(<Thread(Thread-1, started 5868)>, 80)
(<Thread(Thread-5, started 5748)>, 81)
(<Thread(Thread-2, started 7152)>, 82)
(<Thread(Thread-3, started 6348)>, 83)
(<Thread(Thread-4, started 6056)>, 84)
(<Thread(Thread-1, started 5868)>, 85)
(<Thread(Thread-5, started 5748)>, 86)
(<Thread(Thread-2, started 7152)>, 87)
(<Thread(Thread-3, started 6348)>, 88)
(<Thread(Thread-4, started 6056)>, 89)
(<Thread(Thread-1, started 5868)>, 90)
(<Thread(Thread-5, started 5748)>, 91)
(<Thread(Thread-2, started 7152)>, 92)
(<Thread(Thread-3, started 6348)>, 93)
(<Thread(Thread-4, started 6056)>, 94)
(<Thread(Thread-1, started 5868)>, 95)
(<Thread(Thread-5, started 5748)>, 96)
(<Thread(Thread-2, started 7152)>, 97)
(<Thread(Thread-3, started 6348)>, 98)
(<Thread(Thread-4, started 6056)>, 99)
(<Thread(Thread-1, started 5868)>, 100)
(,1)
(, 2)
(, 3)
(, 4)
(, 5)
(, 6)
(, 7)
(, 8)
(, 9)
(, 10)
(, 11)
(, 12)
(, 13)
(, 14)
(, 15)
(, 16)
(, 17)
(, 18)
(, 19)
(, 20)
(, 21)
(, 22)
(, 23)
(, 24)
(, 25)
(, 26)
(, 27)
(, 28)
(, 29)
(, 30)
(, 31)
(, 32)
(, 33)
(, 34)
(, 35)
(, 36)
(, 37)
(, 38)
(, 39)
(, 40)
(, 41)
(, 42)
(, 43)
(, 44)
(, 45)
(, 46)
(, 47)
(, 48)
(, 49)
(, 50)
(, 51)
(, 52)
(, 53)
(, 54)
(, 55)
(, 56)
(, 57)
(, 58)
(, 59)
(, 60)
(, 61)
(, 62)
(, 63)
(, 64)
(, 65)
(, 66)
(, 67)
(, 68)
(, 69)
(, 70)
(, 71)
(, 72)
(, 73)
(, 74)
(, 75)
(, 76)
(, 77)
(, 78)
(, 79)
(, 80)
(, 81)
(, 82)
(, 83)
(, 84)
(, 85)
(, 86)
(, 87)
(, 88)
(, 89)
(, 90)
(, 91)
(, 92)
(, 93)
(, 94)
(, 95)
(, 96)
(, 97)
(, 98)
(, 99)
(, 100)
当然可以循环
lock = threading.Lock()
count = 0
def worker():
"""thread worker function"""
global count
while True:
with lock:
if count >= 100: break
count += 1
print(count)
请注意使用
threading.Lock对count
的受保护访问;依靠是粗略的。老实说,如果你想要这样的东西,那就意味着你走错了方向。因为这段代码是有状态的,stateful
和stateful
处理与真正的并行执行相去甚远。
另外,如果您想在python中并行执行代码,通常需要使用模块
因此,基本上,如果您的目标是总共勾选100次,那么最好以无状态方式重写代码:
import multiprocessing as mp
def worker_1(x):
for i in range(x)
print i
def worker_2(y):
print y
if __name__ == '__main__':
p = mp.Pool(5)
for x in p.pool(worker_1, [25, 25, 25, 25]):
// process result
pass
for y in p.pool(worker_2, range(100)):
// process result
pass