import threading
import time
import random
import string
def checkEvent(mutex):
for i in range(1000):
if mutex.locked():
mutex.release()
print("Mutex released", "=" * 30)
else:
mutex.acquire()
print("Mutex locked", "=" * 30)
time.sleep(0.5)
def longFunc(string, mutex):
while len(string) > 10:
print(string[:10])
string = string[10:]
time.sleep(0.5)
while mutex.locked():
time.sleep(0.1) # or do whatever you need
else:
print(string)
if __name__ == "__main__":
s = "".join(random.choices(string.ascii_uppercase + string.digits, k=1000))
mutex = threading.Lock()
t1 = threading.Thread(target = longFunc, args = (s, mutex))
t2 = threading.Thread(target = checkEvent, args = (mutex, ))
t1.start()
t2.start()
t1.join()
t2.join()
Но ресурсы тут на английском, вроде, все.
Переводите хотя бы Гуглом, там нет ничего сложного