Does anyone know a way to acquire a lock so that not only the called function but also other functions are locked.
Let's say I have a bunch of functions ( a(),b(),c() ) an when I call function a() I want also functions b() & c() to be locked.
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()
How would I have to use the lock if i wanted the above code to give the output:
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
?
Any help appreciated, Greetz
You must in a way or another use the same Lock
instance shared between your functions.
lock = threading.Lock()
def a():
with lock:
for i in range(5):
print(i)
sleep(1)
Do this for each function and it will output what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.