多处理 - 打印输出过程不一致

时间:2016-07-30 18:11:49

标签: python python-2.7 multiprocessing stdout

在使用map时,我无法从流程池中的流程中获取一致的日志记录。

以下是进程池中调用的函数:

from multiprocessing import Pool, Lock
lock = Lock()

def ping(host):
name, address = host

with lock:
    sys.stdout.write("\npinging " + name + " at address " + address)
    sys.stdout.flush()

pop = Popen(['ping', '-n', '1', address], stdout=PIPE)

pop.communicate() # We use communicate to not block the stdin stream, since we are going to need the return code from it
res = pop.wait() # This gets us the return code we want

status = "OK"

if res != 0:
    status = "FAILED"

with lock:
    sys.stdout.write("\n" + name + " , " + address + " , " + status + "\n")
    sys.stdout.flush()

return (name, address, status)

游泳池声明:

pool = Pool(processes=4)
results = pool.map(ping, hosts)

print results

出于某种原因,我从这里获得相当不一致的输出。我认为这是因为不止一个进程会尝试在给定时刻进行打印,但不应该Lock()解决这个问题吗?

以下是我获得的输出类型:

这个我认为好:

pinging CP_RESERVED_MS at address 12.13.14.15
pinging test at address 1.1.1.1
pinging test_diplicate at address 1.1.1.2


CP_RESERVED_MS , 12.13.14.15 , FAILED
test_diplicate , 1.1.1.2 , FAILED
test , 1.1.1.1 , FAILED
[(u'CP_RESERVED_MS', u'12.13.14.15', 'FAILED'), (u'test', u'1.1.1.1', 'FAILED'), (u'test_diplicate', u'1.1.1.2', 'FAILED')]

然后它突然变得混乱:

pinging CP_RESERVED_MS at address 12.13.14.15pinging test at address 1.1.1.1
pinging test_diplicate at address 1.1.1.2

test_diplicate , 1.1.1.2 , FAILED

CP_RESERVED_MS , 12.13.14.15 , FAILED
test , 1.1.1.1 , FAILED
[(u'CP_RESERVED_MS', u'12.13.14.15', 'FAILED'), (u'test', u'1.1.1.1', 'FAILED'), (u'test_diplicate', u'1.1.1.2', 'FAILED')]

不会换行,缺少新线等

为什么会这样?

使用Windows 10 64位

0 个答案:

没有答案