Two uses python lock

Two uses python lock

Program lock function:
use multiple threads in the program, the critical area of the lock processing in order to avoid race conditions occur.

1. Basic usage:

import threading
value_lock = threading.Lock()
value_lock.acquire()
#需要加锁的代码段
value_lock.release()

2. and with match usage

import threading
value_lock = threading.Lock()
with value_lock:
	#需要加锁的代码段

The comparison of the two uses:
Use with a more elegant and less error-prone: using the first method might forget to use the case of release () appears. with statement guaranteed to always release the lock.

Published 30 original articles · won praise 37 · views 4072

Guess you like

Origin blog.csdn.net/qq_32188669/article/details/103189284