2016-04-09 4 views
0

、ここでは簡単な例です。その、マルチプロセスでは、すべてのプロセスでマルチスレッドが生成されるのはなぜですか?私のpython <a href="https://docs.python.org/2/library/multiprocessing.html" rel="nofollow"><code>multiprocessing</code></a>を使用しています

あなたが見ることができるAW
# ps -eLaf | grep test_multi 
cuidehe 4119 4118 4119 2 4 11:06 pts/25 00:00:00 python test_multi.py 
cuidehe 4119 4118 4121 0 4 11:06 pts/25 00:00:00 python test_multi.py 
cuidehe 4119 4118 4122 0 4 11:06 pts/25 00:00:00 python test_multi.py 
cuidehe 4119 4118 4123 0 4 11:06 pts/25 00:00:00 python test_multi.py 
cuidehe 4120 4119 4120 0 1 11:06 pts/25 00:00:00 python test_multi.py 

が、私はただ一つのプロセスをフォーク:

from multiprocessing import Pool 
import time 
import signal 

def process(_id): 
    time.sleep(2) 
    return _id 

def init_worker(): 
    signal.signal(signal.SIGINT, signal.SIG_IGN) 

def main(): 
    pool = Pool(1, init_worker) 
    for res in pool.imap(process, range(1000)): 
     print res 

if __name__ == "__main__": 
    main() 

これは何私を混同することで、[OK]を実行しますpidは4120なので、pid 4119はメインプロセスだと思いますが、なぜ4スレッドですか?この時間は6である

pool = Pool(1, init_worker) 
cursor = parse_db["jd_raw"].find({"isExpired": 0}, 
     {"jdJob.jobPosition": 1, "jdJob.jobDesc": 1, "jdFrom": 1}, no_cursor_timeout=True).\ 
       batch_size(15) 

for res in pool.imap(process, cursor): 
    pass 

cuidehe 4522 2655 4522 21 6 11:28 pts/25 00:00:00 python test_multi_mongo.py 
cuidehe 4522 2655 4525 0 6 11:28 pts/25 00:00:00 python test_multi_mongo.py 
cuidehe 4522 2655 4527 0 6 11:28 pts/25 00:00:00 python test_multi_mongo.py 
cuidehe 4522 2655 4528 54 6 11:28 pts/25 00:00:01 python test_multi_mongo.py 
cuidehe 4522 2655 4529 46 6 11:28 pts/25 00:00:00 python test_multi_mongo.py 
cuidehe 4522 2655 4530 0 6 11:28 pts/25 00:00:00 python test_multi_mongo.py 
cuidehe 4526 4522 4526 28 1 11:28 pts/25 00:00:00 python test_multi_mongo.py 

そしてまた、だけでなく、main processが出現します指摘する

一つのことは、例えばない、常に4つのスレッド、ということです子プロセスも子スレッドを生成するので、なぜマルチプロセスで子スレッドを生成する必要があるのですか?

答えて

2

multiprocessingモジュールは、メインプログラムを続行しながら、バックグラウンドでPoolを管理するために3つの別々のスレッドを使用します。 Pythonインストールのmultiprocessing/pool.pyを参照してください。

関連する問題

 関連する問題