简体   繁体   中英

Does lock.acquire blocks processes in non critical section

Maybe I didn't read the docs that well, but I didn't find more information about how lock's or rlock's aquire work ... does it block all processes no matter what statements those processes are on (even if they are on no critical sections).. or does it block only the processes trying to access the critical section

Thank you !

From the docs:

class multiprocessing.Lock

A non-recursive lock object: a close analog of threading.Lock. Once a process or thread has acquired a lock, subsequent attempts to acquire it from any process or thread will block until it is released; any process or thread may release it. The concepts and behaviors of threading.Lock as it applies to threads are replicated here in multiprocessing.Lock as it applies to either processes or threads, except as noted.

So when you call acquire() (note the use of default value for the block parameter) your process will:

  1. Acquire the lock if it is in an unlocked state.
  2. Block till the lock is in an unlocked state, then acquire it.

This mechanism allows you to define "critical sections" in your logic, meaning that only one process at a time will be executing that particular function (ie playing an audio file)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM