Locks and Logging
Let's see how locks and logging work using the multiprocessing module.
We'll cover the following...
Lock
The multiprocessing module supports locks in much the same way as the
threading module does. All you need to do is import Lock
, acquire
it, do something and release it.
How multiprocessing module supports locks
Let’s take a look:
Press + to interact
from multiprocessing import Process, Lockdef printer(item, lock):"""Prints out the item that was passed in"""lock.acquire()try:print(item)finally:lock.release()if __name__ == '__main__':lock = Lock()items = ['tango', 'foxtrot', 10]for item in items:p = Process(target=printer, args=(item, lock))p.start()
Here we create a simple printing ...