0

I am trying to write a python code where I can employ python multiprocessing. The actual code is long and complicated so I will try to explain my problem using a simple scenario:

consider the following function:

def Parfunc(x):
    print ('abcd')
    return x*x*random.random()

When I employ this function with a for loop it not only return the value but also does the print 'abcd'. Now when I do the same using the following code, it does return the values but the print function doesn't seem to work:

from multiprocessing import Pool
if __name__ == '__main__':
    pool = Pool(processes=4)
    print(pool.map(Parfunc, range(1,21)))

any help would be appreciated...

aws
  • 1
  • 1
    Why do you need `print()` to work in child processes? These all run in parallel and handling stdin / stdout between those is outside the scope of the `multiprocessing` module. – Martijn Pieters Sep 23 '15 at 10:40
  • See [Log output of multiprocessing.Process](http://stackoverflow.com/q/1501651) for an alternative. – Martijn Pieters Sep 23 '15 at 10:41
  • Thanks Martijn. I think I need to explain a bit more. I have written a class and am trying to generate a large number of instances using a for-loop. Now this is taking way too long so what I am trying to do is to write a function that has all the steps to create these instances (and manipulate their attributes). This function works fine if I again use a for loop but doesn't work if I use pool.map (i.e. the instances are not generated). At the end I want class instances generated that I can use later in my program. Thanks!! – aws Sep 23 '15 at 11:35
  • BTW I am not an experienced programmer and using multiprocessing for the first time... – aws Sep 23 '15 at 11:37

0 Answers0