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:
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()
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