Recently, I've been experimenting with the multiprocessing module. I wrote this script to test it:
import multiprocessing
from time import sleep
import datetime
def b(m):
print(m)
def int_val(a):
b(a)
def char_val(a):
sleep(15)
b(a)
list_val = [1,2,'c',6,10,1,'e',11,78,'a', 'b']
if __name__ == '__main__':
p = multiprocessing.Pool(4)
for index, val in enumerate(list_val):
if isinstance(val, str):
p.map(char_val, [val])
print(datetime.datetime.now())
else:
p.map(int_val, [val])
print(datetime.datetime.now())
The output looks like this :
1
2020-03-29 01:45:37.099114
2
2020-03-29 01:45:37.099114
c
2020-03-29 01:45:52.114733
6
2020-03-29 01:45:52.114733
10
2020-03-29 01:45:52.114733
1
2020-03-29 01:45:52.114733
e
2020-03-29 01:46:07.115963
11
2020-03-29 01:46:07.115963
78
2020-03-29 01:46:07.115963
a
2020-03-29 01:46:22.117232
b
2020-03-29 01:46:37.118046
If we see the output, the program has not really achieved multi-processing I was looking for.
I was hoping that while the program waits for the 15 seconds for the element 'c', other elements will be processed and almost all the characters will be printed at the same time.
It might be a silly question, but I am really lost!
Any help would be highly appreciated!