Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System-wide mutex in Python on Linux

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 contrast to a traditional mutex, which is used by a group of threads within the same process.

EDIT: I'm not sure Python's multiprocessing package is what I need. For example, I can execute the following in two different interpreters:

from multiprocessing import Lock L = Lock() L.acquire() 

When I execute these commands simultaneously in two separate interpreters, I want one of them to hang. Instead, neither hangs; it appears they aren't acquiring the same mutex.

like image 404
emchristiansen Avatar asked Aug 03 '11 18:08

emchristiansen


People also ask

Does Python have mutex?

Python provides a mutual exclusion lock via the threading. Lock class. An instance of the lock can be created and then acquired by threads before accessing a critical section, and released after the critical section. Only one thread can have the lock at any time.

What is mutex in Linux?

A Mutex is a lock that we set before using a shared resource and release after using it. When the lock is set, no other thread can access the locked region of code.

How do you implement mutex in Python?

To implement mutex in Python, we can use the lock() function from the threading module to lock the threads. If the second thread is about to finish before the first thread, it will wait for the first thread to finish. We lock the second thread to ensure this, and then we make it wait for the first thread to finish.

Can we share mutex between processes?

Mutexes can synchronize threads within the same process or in other processes. Mutexes can be used to synchronize threads between processes if the mutexes are allocated in writable memory and shared among the cooperating processes (see mmap(2)), and have been initialized for this task.


1 Answers

The "traditional" Unix answer is to use file locks. You can use lockf(3) to lock sections of a file so that other processes can't edit it; a very common abuse is to use this as a mutex between processes. The python equivalent is fcntl.lockf.

Traditionally you write the PID of the locking process into the lock file, so that deadlocks due to processes dying while holding the lock are identifiable and fixable.

This gets you what you want, since your lock is in a global namespace (the filesystem) and accessible to all processes. This approach also has the perk that non-Python programs can participate in your locking. The downside is that you need a place for this lock file to live; also, some filesystems don't actually lock correctly, so there's a risk that it will silently fail to achieve exclusion. You win some, you lose some.

like image 189
zmccord Avatar answered Sep 28 '22 18:09

zmccord