2

I was trying out multiprocessing because I was trying to imitate an internet and how it's nodes work. I am fine with the basic functionality so I looked on the documentation and used the most basic example on the 3.7.0 documentation, the version I am using now. To my surprise, it didn't work. I am using a Mac OS High Sierra, version 10.13.6, if it has to do with anything. Here is the code for clarity:

from multiprocessing import Pool

def f(x):
    return x*x

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

I was wondering why it didn't work, as it didn't show anything, and would want the working version, thank you.

For Those Who Doubt It

Aphrodite
  • 111
  • 1
  • 8
  • It worked for me and printed ```[1, 4, 9]``` running your code as is. I don't understand what's the issue here. – Deepak Saini Sep 01 '18 at 11:43
  • What specifically "didn't work"? – nz_21 Sep 01 '18 at 11:49
  • @nz_21 Look at the picture. – Aphrodite Sep 01 '18 at 11:51
  • Most likely duplicate of [Why doesn't `print` work in Python multiprocessing pool.map - Stack Overflow](https://stackoverflow.com/questions/23480498/why-doesnt-print-work-in-python-multiprocessing-pool-map) (although I can't tell if that window is IDLE) – user202729 Oct 04 '21 at 08:32

3 Answers3

1

pip install multiprocess

from multiprocess import Pool

I used multiprocessing package like most people, but it didn't work. So I tried multiprocess package, and it worked well.

Orestis Zekai
  • 897
  • 1
  • 14
  • 29
Ethan
  • 11
  • 1
0

The screenshot indicates that you've opened up your interpreter for some reason.

Run your file like so: python3 main.py

nz_21
  • 6,140
  • 7
  • 34
  • 80
  • Check out this tutorial: https://www.learnpython.org/ Also, if the answer works, mark it as accepted :) – nz_21 Sep 01 '18 at 11:55
  • @Aphrodite For some reason `multiprocessing` doesn't like interactive python sessions ('interpreter'). Defining a function during such a session means it cannot be used by `multiprocessing`. Not sure why, but that's the way it is. – JoseOrtiz3 Apr 09 '19 at 05:26
0

For me, the issue was using:

random.seed = <SOME INT>

along with any form of multiprocessing.

Anon
  • 619
  • 1
  • 9
  • 18