Python: Locks from `threading` and` multiprocessing` are interchangeable?

Are locks from the threading module interchangeable with multiprocessing modules?

+7
python multithreading locking multiprocessing
source share
3 answers

You can usually use the two interchangeably, but you need to be aware of the differences. For example, multiprocessing.Event is supported by a semaphore that is platform-sensitive under the application.

Multiprocessing.Lock is supported by Multiprocessing.SemLock - so it needs semaphores with a name. In essence, you can use them interchangeably, but using multiprocessor gateways introduces some requirements for the application platform (namely, it does not work on BSD :))

+7
source share

I do not think so. Threaded locks are in the same process, while multiprocessing locks are likely to be in shared memory.

The last time I checked, multiprocessing does not allow you to share a lock in the queue, which is a thread lock.

+1
source share

Yes, you can use locks from the multiprocessing module as usual in your uniprocessor application, but if you use multiprocessing, you should use its locks.

+1
source share

All Articles