0

When I try run something simple from documantaions like: https://docs.python.org/3.4/library/multiprocessing.html?highlight=process

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]))

It runs the code infinite time. I've tried to add close and join, and tried to get id of procceses but nothing worked. Seems like it looping pool over and over and cant execute it. What is wrong?

from datetime
import datetime from datetime
import date
import os
import time
from multiprocessing import Pool
def y(z):
    print(f'Process {os.getpid()} working record {z}')
    time.sleep(1)
    q = z*2
    print(f'Process {os.getpid()} done record {q}')
    return(q)

with Pool(2) as p:
    result = p.map(y, [1, 2, 3])
    p.close()
    p.join()
end_time = datetime.now() 
print('Duration: {}'.format(end_time - start_time))
######Error
Process SpawnPoolWorker-1:
Process SpawnPoolWorker-2:
Traceback (most recent call last):
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py", line 110, in worker
    task = get()
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py", line 354, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'y' on <module '__main__' (built-in)>
Traceback (most recent call last):
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py", line 110, in worker
    task = get()
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py", line 354, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'y' on <module '__main__' (built-in)>
Process SpawnPoolWorker-3:
Traceback (most recent call last):
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py", line 110, in worker
    task = get()
  File "C:\Users\a.ovchinnikov\AppData\Local\Continuum\anaconda3\lib\multiprocessing\queues.py", line 354, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'y' on <module '__main__' (built-in)>
Alex Ov
  • 11
  • 3
  • ^ You should add that to your question, you can use the edit button. It will be easier to read as part of the question (using code formatting) instead of a comment – Flight Odyssey Nov 26 '18 at 06:02

1 Answers1

0

Both of your code samples run fine on my machine in python3, after fixing a couple syntax errors in the second one that seem to just be copy-paste issues. (See edited code below.) Maybe there is something misconfigured on your computer or you are not using the correct command to run them? Are you able to successfully run other python programs?

from datetime import datetime
from datetime import date
import os
import time
from multiprocessing import Pool
def y(z):
    print(f'Process {os.getpid()} working record {z}')
    time.sleep(1)
    q = z*2
    print(f'Process {os.getpid()} done record {q}')
    return(q)

start_time = datetime.now() 
with Pool(2) as p:
    result = p.map(y, [1, 2, 3])
    p.close()
    p.join()

end_time = datetime.now() 
print('Duration: {}'.format(end_time - start_time))
Flight Odyssey
  • 2,267
  • 18
  • 25
  • I am using anaconda stack. And yes I am able to run other python programs. But multiprocessing doesnt run. And I don't know where to start. What could be misconfigured? – Alex Ov Nov 26 '18 at 06:35
  • Huh, that's interesting. Then I honestly have no idea why it wouldn't work for you. – Flight Odyssey Nov 26 '18 at 07:11
  • I've find an error.Placed it in a description. – Alex Ov Nov 26 '18 at 12:33
  • Found solution here: https://bugs.python.org/issue25053, https://stackoverflow.com/questions/41385708/multiprocessing-example-giving-attributeerror – Alex Ov Nov 26 '18 at 12:55