1

Code here:

from multiprocessing import pool
def worker(num):
    print 'Worker:', num
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()

Sorry I am new to python. I am getting the below error whenever I try to import pool. It says something wrong with os.chdir(wdir) but I cant figure out what. Any help ?

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\z080302\Desktop\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/z080302/Desktop/Python_Projects/mp_test.py", line 18, in <module>
p = multiprocessing.Process(target=worker, args=(i,))
NameError: name 'multiprocessing' is not defined
Holloway
  • 6,412
  • 1
  • 26
  • 33
Kumar Gavade
  • 71
  • 1
  • 2
  • 12
  • 5
    Have you written your own file called multiprocessing? It might be trying to import that (which doesn't have a `pool` to import). – Holloway Feb 26 '15 at 11:14
  • ok so i had created the multiprocessing file, which is why i think it was giving an error..i deleted that file now. but now it gives a name error..editing the original question – Kumar Gavade Feb 26 '15 at 11:21
  • Can you provide us with the whole file? If it's too big copy-paste it into pastebin and post the link here – no_name Feb 26 '15 at 11:25
  • which file do you want me to share ? I am just running a small code. ` from multiprocessing import pool def worker(num): print 'Worker:', num return if __name__ == '__main__': jobs = [] for i in range(5): p = multiprocessing.Process(target=worker, args=(i,)) jobs.append(p) p.start()` – Kumar Gavade Feb 26 '15 at 11:27
  • I found the problem, all you have to do is to add import multiprocessing under from multiprocessing import pool – no_name Feb 26 '15 at 11:28

4 Answers4

9

Did you call your script 'multiprocessing.py'? Check your directory or file first... if yes -> Python will look for Pool in your script, which probably isn't there.(Rename it if exist) Sounds funny but it happens )

ant_dev
  • 653
  • 8
  • 16
  • multiprocessing is a python library.. https://docs.python.org/2/library/multiprocessing.html – StupidWolf Nov 15 '19 at 13:29
  • @StupidWolf, Hi Wold, yes it is, but if you try to set your script name to multiprocessing.py it must not work , wait a min I will post a code . – ant_dev Nov 15 '19 at 13:33
  • from multiprocessing import Pool import time COUNT = 50000000 def countdown(n): while n>0: n -= 1 if __name__ == '__main__': pool = Pool(processes=2) start = time.time() r1 = pool.apply_async(countdown, [COUNT//2]) r2 = pool.apply_async(countdown, [COUNT//2]) pool.close() pool.join() end = time.time() print('Time taken in seconds -', end - start) and file name is multiprocessing.py – ant_dev Nov 15 '19 at 13:34
  • yes i understand that there might be a clash. Look carefully at the question, the OP did only "from multiprocessing import pool".. so he/she did not import multiprocessing directly. So most likely you would resolve that first. – StupidWolf Nov 15 '19 at 13:38
  • @StupidWolf True – ant_dev Nov 15 '19 at 13:40
2

Here's your code:

from multiprocessing import pool
def worker(num):
    print 'Worker:', num
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()

You have to import the module multiprocessing to use multiprocessing.Process, you have only imported the function/class Pool from multiprocessing, so a simple fix would be:

import multiprocessing
pool = multiprocessing.pool
def worker(num):
    print 'Worker:', num
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()
no_name
  • 742
  • 3
  • 10
  • thanks..can you point me somewhere to learn how multiprocessing actually works ?? would be of great help – Kumar Gavade Feb 26 '15 at 11:45
  • 1
    Certainly : http://sebastianraschka.com/Articles/2014_multiprocessing_intro.html , http://pymotw.com/2/multiprocessing/basics.html – no_name Feb 26 '15 at 11:47
2

You are importing only pool module from multiprocessing module. So your interpreter is aware of pool only and not of multiprocessing

To resolve the problem, you must import multiprocessing and when you require the pool in code you can use it like multiprocessing.pool

multiprocessing
    |-- __init__.py
    |--Process
    |--Pool
    |--This_also

Like shown above, you are importing only pool and python doesn't know who the hell this multiprocessing and Process and This_also are. Usually we have __ init __.py file in python package. A list in this file all = ['pool.py','Process.py',.......'This_also'] contains all the modules contained in the package. So import * will import all the modules. Please go through the https://docs.python.org/2/tutorial/modules.html#

ajay_t
  • 2,347
  • 7
  • 37
  • 62
1

If you have written a file called multiprocessing.py, it will try to import that first, which will mean it will not find Pool in it when you do "from multiprocessing import Pool".

multiprocessing is a module included in Python and has Pool in it, so if there are no conflicts the below should work: "from multiprocessing import Pool"

JakeJ
  • 2,361
  • 5
  • 23
  • 35