Reputation: 2214
I'm trying to understand the Lock Object in Python. For this, I have programmed, something like the python multithreading.Pool object. However I'm still a bit wondering why the lock don't really seem to lock the execution of the other Processes. If I replace class DoSomething(multiprocessing.Process): by class DoSomething(threading.Thread): this works perfectly (yeah, ok, only if there's no exception, because pop don't work on an empty list); but I'm not sure, wheather this is because of the lock, or because of the shared memory.
#! /usr/bin/env python
import multiprocessing, threading
lock = multiprocessing.Lock()
class DoSomething(multiprocessing.Process):
def __init__(self, jobList, lock):
super(DoSomething, self).__init__()
self.jobList, self.lock = jobList, lock
def run(self):
while jobList != []: # while there're still jobs available
self.lock.acquire() # lock the jobList
job = self.jobList.pop() # and take the next job (in fact, pop removes the
print job # lst object from the list and returns it).
self.lock.release() # release the jobList, so that another thread may do another job
# do something with this job ...
threads = []
jobList = [1,2,3,4]
for i in range(4):
threads.append(DoSomething(jobList, lock))
threads[-1].start()
for t in threads:
t.join()
The output of this program is:
$ python multThread.py
4
3
2
1
4 // so it don't lock, we have calculated the job twice here!
3
2
1
// ...
Upvotes: 0
Views: 208
Reputation: 522
The lock is working. The problem is that, with multiprocessing, the jobList
is not in shared memory like it is with threading. In multiprocessing, the separate processes have a copy of the jobList
. Try making the jobList
a multiprocessing.Array. See Sharing state between processes for more info.
Upvotes: 1