wieloprocesowe przetwarzanie python3.x bez „if __name__ ==” __main__ ”:”
Mam ten plik (nie czyni żadnej użytecznej pracy, jest tylko do nauki):
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()
w tej formie działa doskonale:
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
ale jeśli wstawię plik zamiast
if __name__ == '__main__':
parent()
tylko
parent()
wchodzi w cykl ...
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):
Czemu?! Co innego sprawia, że klauzula if?