I have a python program as follows
import multiprocessing
import time
icout=1
def send_request(data):
lock.acquire()
time.sleep(0.5)
global icout
icout=icout+1
print(icout)
lock.release()
def init(l):
global lock
lock = l
if __name__ == '__main__':
data_list = ['data1', 'data2', 'data3']
lock = multiprocessing.Lock()
pool = multiprocessing.Pool(3, initializer=init, initargs=(lock,))
pool.map(send_request, data_list)
pool.close()
pool.join()
When I comment out the statement time.sleep(0.5)
,it will print 1,2,3.
When I keep the statement time.sleep(0.5)
,it will print 2,2,2
my question is When I keep this statement time.sleep(0.5)
, why is the output of the program not 1, 2, 3
I have locked it, it seems that the lock does not work.