I am trying python multiprocessing using Python 3.7.4 on Windows 10, Intel Core i7-8550U processor.
I am testing the multiprocessing with two functions, one with a basic sleep() and another with using the matthews_corrcoef from sklearn. The multiprocessing works with the sleep function, but not with the sklearn function.
import numpy as np
from sklearn.metrics import matthews_corrcoef
import time
import concurrent.futures
from multiprocessing import Process, Pool
from functools import partial
import warnings
import sys
class Runner():
def sleeper(self, pred, man, thr = None):
return time.sleep(2)
def mcc_score(self, pred, man, thr = None):
warnings.filterwarnings("ignore")
return matthews_corrcoef(pred, man)
def pool(self, func):
t1 = time.perf_counter()
p = Pool()
meth = partial(func, pred, man)
res = p.map(meth, thres)
p.close()
t2 = time.perf_counter()
print(f'Pool {func.__name__} {round((t2-t1), 3)} seconds')
def vanilla(self, func):
t1 = time.perf_counter()
for t in thres:
func(pred, man)
t2 = time.perf_counter()
print(f'vanilla {func.__name__} {round((t2-t1), 3)} seconds')
if __name__== "__main__":
print(sys.version)
r = Runner()
thres = np.arange(0,1, 0.3)
print(f"Number of thresholds {len(thres)}")
pred = [1]*200000
man = [1]*200000
results = []
r.pool(r.mcc_score)
r.vanilla(r.mcc_score)
r.pool(r.sleeper)
r.vanilla(r.sleeper)
In windows, for the mcc_score function, using pool is actually slower than the vanilla version, whereas in Linux it works properly.
Here are the sample outputs
#windows
3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Number of thresholds 4
Pool mcc_score 3.247 seconds
vanilla mcc_score 1.591 seconds
Pool sleeper 5.828 seconds
vanilla sleeper 8.001 seconds
#linux
3.7.0 (default, Jun 28 2018, 13:15:42) [GCC 7.2.0]
Number of thresholds 34
Pool mcc_score 1.946 seconds
vanilla mcc_score 8.817 seconds
I went through the documentation and other relevant questions in stackoverflow, where it mainly states using if __name__== "__main__":
. Some help would be greatly appreciated as I have been stuck on this for quite some time now. If I missed any important information, please mention it, I will provide it.