Home > Backend Development > Python Tutorial > How to Share a Lock Between Processes in Python Using Multiprocessing

How to Share a Lock Between Processes in Python Using Multiprocessing

Linda Hamilton
Release: 2024-10-17 11:12:02
Original
934 people have browsed it

How to Share a Lock Between Processes in Python Using Multiprocessing

Sharing a Lock Between Processes in Python

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.

Option 1: Using Manager and Manager.Lock()

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.

Option 2: Pass Lock at Pool Creation with initializer

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template