Home > Article > Backend Development > How to Share a Lock Between Processes in Python Using Multiprocessing
When attempting to use pool.map() to target a function with multiple parameters, including a Lock() object, it's crucial to address the issue of sharing the lock between subprocesses. The conventional multiprocessing.Lock() cannot be passed directly to Pool methods due to pickling limitations.
One approach is to utilize Manager() and instantiate a Manager.Lock(). While this method is reliable, it involves more overhead due to the additional process that hosts the Manager server. Additionally, lock operations require communication with this server via IPC.
Alternatively, you can pass the regular multiprocessing.Lock() during Pool initialization using the initializer keyword argument. This ensures that the lock instance is global in all child workers. This method eliminates the necessity for partial functions and streamlines the process.
Here's an example using Option 2:
<code class="python">def target(iterable_item): for item in items: # Do cool stuff if (... some condition here ...): lock.acquire() # Write to stdout or logfile, etc. lock.release() def init(l): global lock lock = l def main(): iterable = [1, 2, 3, 4, 5] l = multiprocessing.Lock() pool = multiprocessing.Pool(initializer=init, initargs=(l,)) pool.map(target, iterable) pool.close() pool.join()</code>
The above is the detailed content of How to Share a Lock Between Processes in Python Using Multiprocessing. For more information, please follow other related articles on the PHP Chinese website!