This example program of using the multiprocessing is crashing my i7 4 core windows7 64 bit computer (32 bit enthought Python 2.7), but is running fine in virtual machine Ubuntu with 4 cores assigned. Can anyone run it in their commputer in Windows7. My system memory (8 GB) is totally eaten by the program and mouse moves but I can not do anything with computer, even ctrl-alt-del to get to task manager.

# -*- coding: utf-8 -*-
from numpy import arange,sqrt, random, linalg
from multiprocessing import Pool

global counter
counter = 0
def cb(r):
    global counter
    print counter, r
    counter +=1

def det(M):
    return linalg.det(M)

po = Pool()
for i in xrange(1,300):
    j = random.normal(1,1,(100,100))
    po.apply_async(det,(j,),callback=cb)
po.close()
po.join()
print counter

http://pyinsci.blogspot.fi/2009/02/usage-pattern-for-multiprocessing.html

I found out that in windows the creation of Pool must be guarded by

if __name__ == '__main__': 

as the module is imported by interpreter. So this works:

# -*- coding: utf-8 -*-
from numpy import arange,sqrt, random, linalg
from multiprocessing import Pool

global counter
counter = 0
def cb(r):
    global counter
    print counter, r
    counter +=1

def det(M):
    return linalg.det(M)

if __name__ == '__main__':
    po = Pool(2)
    for i in xrange(1,100):
        j = random.normal(1,1,(100,100))
        po.apply_async(det,(j,),callback=cb)
    po.close()
    po.join()
    print counter

Thanks for letting us know!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.