5

I have some code which runs in parallel using the multiprocessing Pool class. Unfortunately some of the functions I use from another library have some verbose output. To abstract the problem have a look at the following example:

from multiprocessing import Pool def f(x): print 'hello' return x*x p = Pool(10) p.map(f, [1,2,3]) 

So this will print 'hello' like 10 times. Is there a possibility to mute the output of the processes or to put the std.out in some variable? Thanks for any suggestion.

EDIT: I don't want to redirect the whole stdout, just for the processes of my pool.

3
  • possible duplicate of Redirect stdout to a file in Python? Commented Jun 14, 2015 at 13:29
  • what about this one? It suggests a contextmanager. Commented Jun 14, 2015 at 13:32
  • It doesn't work I think the problem is that multiple independent processes are spawned. Commented Jun 14, 2015 at 13:36

1 Answer 1

11

Use the initializer parameter to call mute in the worker processes:

import sys import os from multiprocessing import Pool def mute(): sys.stdout = open(os.devnull, 'w') def f(x): print 'hello' return x*x if __name__ == '__main__': p = Pool(10, initializer=mute) p.map(f, [1,2,3]) print('Hello') 

prints

Hello 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.