Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Locks from `threading` and `multiprocessing` interchangable?

Are the locks from the threading module interchangeable with those from the multiprocessing module?

like image 278
Ram Rachum Avatar asked Apr 11 '09 07:04

Ram Rachum


2 Answers

You can typically use the two interchangeably, but you need to cognizant of the differences. For example, multiprocessing.Event is backed by a named semaphore, which is sensitive to the platform under the application.

Multiprocessing.Lock is backed by Multiprocessing.SemLock - so it needs named semaphores. In essence, you can use them interchangeably, but using multiprocessing's locks introduces some platform requirements on the application (namely, it doesn't run on BSD :))

like image 104
jnoller Avatar answered Nov 03 '22 02:11

jnoller


I don't think so. Threading locks are within the same process, while the multiprocessing lock would likely be in shared memory.

Last time I checked, multiprocessing doesn't allow you to share the lock in a Queue, which is a threading lock.

like image 22
Unknown Avatar answered Nov 03 '22 01:11

Unknown