python3.x Multiprocessing-Zyklus ohne "if __name__ == '__main__':"
Ich habe diese Datei (es macht keine nützliche Arbeit, es ist nur zum Lernen):
import multiprocessing,sys
def parent(numproc=2):
print ('at start')
childs=[]
print ('bfore Pipe')
(parentEnd,childEnd)=multiprocessing.Pipe()
i=0
print ('printing i:',i)
child=multiprocessing.Process(target=child_proc, args=(childEnd,i))
print ('created child')
child.start()
print ('started child')
print ('joining child')
child.join()
print ('joined child')
print ('exeted from for i in childs')
mins=[1,2]
print ('task ended. result: ',min(mins))
def child_proc(pipe,name):
pass
if __name__ == '__main__':
parent()
In dieser Form läuft es perfekt:
at start
bfore Pipe
printing i: 0
created child
started child
joining child
joined child
exeted from for i in childs
task ended. result: 1
aber wenn ich statt dateiende stecke
if __name__ == '__main__':
parent()
nur
parent()
es fällt in Zyklus ...
at start
bfore Pipe
printing i: 0
created child
started child
joining child
at start
bfore Pipe
printing i: 0
created child
started child
joining child
at start
bfore Pipe
printing i: 0
created child
started child
joining child
Traceback (most recent call last):
Warum?! Was unterscheidet diese if-Klausel?