"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Share a Lock Between Processes in Python Using Multiprocessing

How to Share a Lock Between Processes in Python Using Multiprocessing

Published on 2024-11-05
Browse:154

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:

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()
Release Statement This article is reprinted at: 1729132755 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3