ich verwende Python multiprocessing
, hier ist ein einfaches Beispiel:Warum spuckt Multiprozess Multi Threads in jedem Prozess?
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()
dies ok läuft, was mich verwirrt ist, dass:
# 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
aw können Sie sehen, ich habe einen Prozess nur gegabelt, seine pid ist 4120
, so denke ich, dass die pid 4119
ist der Hauptprozess, aber warum 4 Threads?
eine Sache, darauf zu hinweisen, dass, nicht immer 4 Threads, zum Beispiel:
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
diesmal 6:
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
Und auch, nicht nur main process
spawnen Kind-Threads, aber auch Child-Prozess wird Kind-Threads spawnen, also , warum Multiprozess noch Kind Threads spawnen muss?