Python-使用多重处理时输出的多个副本 [英] Python - multiple copies of output when using multiprocessing

查看:69
本文介绍了Python-使用多重处理时输出的多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
多处理启动了太多的Python VM实例

Possible Duplicate:
Multiprocessing launching too many instances of Python VM

通过python myscript.py运行的模块(不是shell输入)

Module run via python myscript.py (not shell input)

import uuid
import time
import multiprocessing


def sleep_then_write(content):
    time.sleep(5)
    print(content)

if __name__ == '__main__':
    for i in range(15):
        p = multiprocessing.Process(target=sleep_then_write,
                                    args=('Hello World',))
        p.start()

print('Ah, what a hard day of threading...')

此脚本输出以下内容:

Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
AAh, what a hard day of threading..
h, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Ah, what a hard day of threading...
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

首先,为什么要打印底部语句而不是一次打印16次(每个过程一次)?

Firstly, why the heck did it print the bottom statement sixteen times (one for each process) instead of just the one time?

第二,注意 AAh, h, 大约一半.那才是真正的输出.这使我对现在永远使用线程保持警惕.

Second, notice the AAh, and h, about half way down; that was the real output. This makes me wary of using threads ever, now.

(Windows XP,Python 2.6.4,Core 2 Duo)

(Windows XP, Python 2.6.4, Core 2 Duo)

推荐答案

多处理通过启动多个进程来工作.每个进程都加载脚本的副本(这样便可以访问目标"功能),然后运行目标功能.

multiprocessing works by starting several processes. Each process loads a copy of your script (that way it has access to the "target" function), and then runs the target function.

您将获得16次底部的打印语句,因为该语句本身就坐在那里,并在加载模块时被打印.将其放在 main 块中,不会:

You get the bottom print statement 16 times because the statement is sitting out there by itself and gets printed when you load the module. Put it inside a main block and it wont:

if __name__ == "__main__":
    print('Ah, what a hard day of threading...')

关于"AAh"-您有多个进程在运行,它们将在运行时产生输出,因此您只需将一个进程中的"A"与另一个进程中的"Ah"相邻即可.

Regarding the "AAh" - you have multiple processes going and they'll produce output as they run, so you simply have the "A" from one process next to the "Ah" from another.

在处理多进程或多线程环境时,您必须考虑锁定和通信.这不是多重处理所独有的.任何并发库都会有相同的问题.

When dealing with multi process or multi threaded environments you have to think through locking and communication. This is not unique to multiprocessing; any concurrent library will have the same issues.

这篇关于Python-使用多重处理时输出的多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆