guiboy
guiboy

Reputation: 111

how to use python's multiprocessing module

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

Upvotes: 2

Views: 2178

Answers (3)

guiboy
guiboy

Reputation: 111

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

Upvotes: 1

João Paulo
João Paulo

Reputation: 6670

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

Upvotes: 1

Zizouz212
Zizouz212

Reputation: 4998

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...

Upvotes: 0

Related Questions