0
あなたが見ることができる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つのスレッド、ということです子プロセスも子スレッドを生成するので、なぜマルチプロセスで子スレッドを生成する必要があるのですか?