【发布时间】:2017-08-06 13:49:20
【问题描述】:
有没有人知道一种获取锁的方法,这样不仅被调用的函数而且其他函数都被锁定。
假设我有一堆函数( a(),b(),c() ),当我调用函数 a() 时,我还希望函数 b() 和 c() 被锁定。
import threading
from time import sleep
def a():
for i in range(5):
print(i)
sleep(1)
def b():
for i in range(5):
print(i)
sleep(1)
def c():
for i in range(5):
print(i)
sleep(1)
thread_a=threading.Thread(target=a)
thread_a.start()
thread_b=threading.Thread(target=b)
thread_b.start()
thread_c=threading.Thread(target=c)
thread_c.start()
如果我希望上面的代码给出输出,我将如何使用锁:
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
?
任何帮助表示赞赏, 问候
【问题讨论】:
标签: python multithreading python-2.7 python-3.x python-multithreading