4

I was working with python 3.6 on Linux, and I was using python's multiprocessing library, It was working until I switch to Windows 10. First, I thought it's because the multiprocessing library doesn't work the same way on Linux and Windows, so I make sure that all my variables are picklable but no results.

Then, I wanted to see if multiprocessing pool is working, I tried this code : (the basic example in multiprocessing doc)

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))

It didn't work and when I check the processors in the task manager, only 1% is used.

I also tried the p.map_async, it worked but when I try to get the results (p.map_async().get()) it doesn't work (RuntimeError)

Blckknght
  • 100,903
  • 11
  • 120
  • 169
Oussama Jabri
  • 674
  • 1
  • 7
  • 18

1 Answers1

-2
from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    results = p.imap(f,range(300))
    for item in results:
        print(item)
    p.close()

this will print items returned in order. if you print(p.imap(f,range(300)) it returns memory location of data. If you say p = Pool() it will start max number of processes your CPU can handle.

Michael Smith
  • 77
  • 1
  • 10