2

I'm using Intel's Pentium(R)Dual-core E5700 @3.00GHz with 2GB ram. I'm trying to learn Python's multiprocessing module. I wrote a simple program that perfoms addition, but its not working; I'm only a getting number of core 2. Is something wrong with my pc or my code?

import multiprocessing
print "number of core ",multiprocessing.cpu_count()



def calc(a,b):
    c= a+b
    return c

if __name__ =='__main__':

    p1 = multiprocessing.Process(target=calc,args=(3,5) )
    p1.start()
    p2 = multiprocessing.Process(target=calc,args=(2,2) )
    p2.start()



p1.join()
p2.join()
dano
  • 91,354
  • 19
  • 222
  • 219
guiboy
  • 101
  • 1
  • 10
  • change `return c` to `print c` – itzMEonTV Feb 15 '15 at 20:34
  • why changed return to print ,it is function its work perfectly when i call it calc(2,3) im getting 5 – guiboy Feb 15 '15 at 20:42
  • 2
    If you have a dual-core machine, then your CPU count is two. What makes you think this is wrong? What are you expecting it to be? – Steven Kryskalla Feb 15 '15 at 20:43
  • k..then how you know the function is executed by your prgm? Also You getting `number of core 2`. Then what is the problem facing in your program? – itzMEonTV Feb 15 '15 at 20:45
  • It's not clear from your question what part of the program isn't working. What behavior are you seeing? What behavior are you expecting to see? – dano Feb 15 '15 at 20:48
  • @ lost-theory i m expecting the result ,do u dont think that i shoud get 8 and 4 as result but im only getting number of core – guiboy Feb 15 '15 at 20:48
  • He's trying to figure out why is function won't output. The only output he is getting is the number of cores he has. Is that right? – Zizouz212 Feb 15 '15 at 20:50
  • if you want result not only to print in terminal, refer http://stackoverflow.com/questions/10797998/is-it-possible-to-multiprocesspython-a-function-that-returns-something – itzMEonTV Feb 15 '15 at 20:58
  • @guiboy No, you won't see 8 and 4 get printed unless you explicitly `print` them. The only thing you print is `"number of core ", multiprocessing.cpu_count()`, so that's all that's going to get printed. – dano Feb 15 '15 at 21:00
  • If I remember correctly, `return` will make it so that the system can use it, but it won't output to the screen. You can use `print` and `return` in the same function, as long as `print` comes before `return` -> Good if you need to see output and make it system usable at the same time. – Zizouz212 Feb 15 '15 at 21:02

3 Answers3

1

I suggest you to use Queue. Look this example:

from multiprocessing import Queue, Process

def calc(a, b, queue):
    queue.put(a + b)

if __name__ == '__main__':
    queue = Queue()
    p1 = Process(target = calc, args = (4, 4, queue,))
    p2 = Process(target = calc, args = (4, 4, queue,))
    p1.start()
    p2.start()
    result_1 = queue.get()
    result_2 = queue.get()
    print(result_1, result_2)
    p1.join()
    p2.join()
    input()

>>> 8 8

The same code dynamically:

from multiprocessing import Queue, Process, cpu_count

def calc(a, b, queue):
    queue.put(a + b)

if __name__ == '__main__':
    queue = Queue()
    processes = []
    for i in range(cpu_count()):
        processes.append(Process(target = calc, args = (4, 4, queue,)))
        processes[-1].start()
    results = []
    for i in range(cpu_count()):
        results.append(queue.get())
    print(results)
    for process in processes:
        process.join()

>>> [8, 8] # if you have two cores
João Paulo
  • 6,300
  • 4
  • 51
  • 80
  • ur both example sometime its work ,sometime its jam ide like while loop,i think multiprocessing dont work properly on windows – guiboy Feb 15 '15 at 21:54
  • it works... check if you aren't using IDLE to run the code and, in boot advanced options on msconfig, the configuration is allowing you to use all cores. – João Paulo Feb 15 '15 at 23:39
1

everything work perfectly including my calc program under pycharm only problem was IDE,i was using pyscripter

guiboy
  • 101
  • 1
  • 10
0

In your calc function, you need to change return to print. On my quad-core machine (running OS X Mavericks), this is my output when running the script in terminal. You should also put p1.join() and p2.join() as part of your if __name__ == "__main__.

Last login: Sun Feb 15 15:47:18 on ttys001
imac:~ zinedine$ cd '/Users/zinedine/Documents/' && '/usr/local/bin/pythonw'
'/Users/zinedine/Documents/example.py'  && echo Exit status: $? && exit 1
number of cores  4
8
4
Exit status: 0
logout

[Process completed]

The code that I used in Terminal...

import multiprocessing

print "number of cores ", multiprocessing.cpu_count()

def calc(a, b):
    c = a + b
    return c

if __name__ == "__main__":
    p1 = multiprocessing.Process(target = calc, args = (3, 5) )
    p1.start()

    p2 = multiprocessing.Process(target = calc, args = (2, 2) )
    p2.start()

    p1.join()
    p2.join()

After using Python Launcher.app, It opens in Terminal and it gives the output that I gave above...

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • i did still not working import multiprocessing print "number of core ",multiprocessing.cpu_count() def calc(a,b): c= a+b print c if __name__ =='__main__': p1 = multiprocessing.Process(target=calc,args=(3,5) ) p1.start() p2 = multiprocessing.Process(target=calc,args=(2,2) ) p2.start() p1.join() p2.join() – guiboy Feb 15 '15 at 20:56
  • Why are you doing `from multiprocessing import Process` and `import multiprocessing`? Just do a simple `import multiprocessing`. But that's besides the point. Do you think you can make an edit to your question describing your output? – Zizouz212 Feb 15 '15 at 20:58
  • this script can only run as python file.Since processing done under `if __name__ =='__main__':`.So there is no need of indentation for `p1.join()` and `p2.join` .It will execute anyways – itzMEonTV Feb 15 '15 at 21:01
  • no, i m not doing import multiprocessing , im doing import multiprocessing u can test my code in ur terminal ,sorry im little confuse – guiboy Feb 15 '15 at 21:02
  • @latheefitzmeontv It's actually a bug to not indent it. On Windows, that code will be executed in each child process, while the code in the `if __name__ ...` block will not. Obviously that is not desirable, since it will make both child processes crash. You'd have the same issue if you were to import the module instead of executing it directly. – dano Feb 15 '15 at 21:03
  • @dano thats correct. If it import, there should be issue.Since he programmed multiprocessing under `if main`. K that is not the issue here ;) – itzMEonTV Feb 15 '15 at 21:13
  • @latheefitzmeontv If you were to try to run that code on Windows, it would be an issue. Windows need to re-import the `__main__` module in each child process created by the `multiprocessing` module in order to execute `calc`. That import will fail because of the indentation issue, so neither child process will execute properly. Things will work fine on Linux/OSX because they support `fork` and don't need to re-import `__main__` in each child. – dano Feb 15 '15 at 21:16
  • @Zizouz212 Your code will always work, because you indented properly. The OP's code will not work on Windows, because the calls to `join()` are not indented properly. – dano Feb 15 '15 at 21:38
  • So, if the OP indents the `join()`, the code will work on Windows? – Zizouz212 Feb 15 '15 at 21:39
  • this code sometime work properly ,sometime its jam ide like in while loop ;from multiprocessing import Queue, Process, cpu_count def calc(a, b, queue): queue.put(a + b) if __name__ == '__main__': queue = Queue() processes = [] for i in range(cpu_count()): processes.append(Process(target = calc, args = (4, 4, queue,))) processes[-1].start() results = [] for i in range(cpu_count()): results.append(queue.get()) print(results) for process in processes: process.join() – guiboy Feb 15 '15 at 21:49
  • sometimes? Oh, make sure you comment on the right question! :) – Zizouz212 Feb 15 '15 at 21:50