System-wide mutex in Python on Linux

后端 未结 6 1400
终归单人心
终归单人心 2020-12-04 21:02

Is there any easy way to have a system-wide mutex in Python on Linux? By \"system-wide\", I mean the mutex will be used by a group of Python processes; this is in c

6条回答
  •  清歌不尽
    2020-12-04 21:39

    The POSIX standard specifies inter-process semaphores which can be used for this purpose. http://linux.die.net/man/7/sem_overview

    The multiprocessing module in Python is built on this API and others. In particular, multiprocessing.Lock provides a cross-process "mutex". http://docs.python.org/library/multiprocessing.html#synchronization-between-processes

    EDIT to respond to edited question:

    In your proof of concept each process is constructing a Lock(). So you have two separate locks. That is why neither process waits. You will need to share the same lock between processes. The section I linked to in the multiprocessing documentation explains how to do that.

提交回复
热议问题