Ответ 1
Вы можете видеть, что ваши блокировки в значительной степени работают, когда вы их используете, если вы замедляете процесс и блокируете их немного больше. У вас была правильная идея, где вы окружали критические фрагменты кода с помощью блокировки. Ниже приведена небольшая корректировка вашего примера, чтобы показать вам, как каждый ждет на другом, чтобы освободить блокировку.
import threading
import time
import inspect
class Thread(threading.Thread):
def __init__(self, t, *args):
threading.Thread.__init__(self, target=t, args=args)
self.start()
count = 0
lock = threading.Lock()
def incre():
global count
caller = inspect.getouterframes(inspect.currentframe())[1][3]
print "Inside %s()" % caller
print "Acquiring lock"
with lock:
print "Lock Acquired"
count += 1
time.sleep(2)
def bye():
while count < 5:
incre()
def hello_there():
while count < 5:
incre()
def main():
hello = Thread(hello_there)
goodbye = Thread(bye)
if __name__ == '__main__':
main()
Пример вывода:
...
Inside hello_there()
Acquiring lock
Lock Acquired
Inside bye()
Acquiring lock
Lock Acquired
...