python编程中的if __name__ == 'main与windows中使用多进程
if __name__ == 'main
一个python的文件有两种使用的方法,第一是直接作为程序执行,第二是import到其他的python程序中被调用(模块重用)执行。
因此if __name__ == 'main': 的作用就是控制这两种情况执行代码的过程,
在if __name__ == 'main': 下的代码只有在第一种情况下(即文件作为程序直接执行)才会被执行,而import到其他程序中是不会被执行的
第一种情况:
def func(): print('second') print('first') print(__name__) # 当运行自身被执行的时候,__name__=__main__ if __name__ == '__main__': func() #当单独执行该文件时,即python main_test.py,输出结果: """ first __main__ second """
if __name__=="__main__": 语句之前和之后的代码都被执行。
第二种情况:
import main_test # 导入main_test文件 if __name__ == '__main__': print('main_test2') # 当单独执行该文件时,即python mian_test2.py,输出结果 """ first # main_test模块中的print main_test # 当main_test被导入时, main_test.py中print(__name__) 输出为导入模块的名称main_test,而不是__main__。 main_test2 """
second没有被输出?
main_test被导入后,不满足__name__ == '__main__' 所以不会输出second,即main_test中的if __name__=="__main__": 之前的语句被执行,之后的没有被执行
Windows创建多进程
代码一
import multiprocessing import time import threading print('main process start1') def func(): print('subprocess start',time.asctime(time.localtime(time.time()))) time.sleep(3) print('subprocess end',time.asctime(time.localtime(time.time()))) p = multiprocessing.Process(target=func, name='1号') p.start() print('main process end', time.asctime(time.localtime(time.time())),multiprocessing.current_process()) if __name__ == '__main__': pass # 默认所有进程结束后,程序才结束
执行效果
main process start1 main process end Fri Mar 23 11:04:46 2018 <_MainProcess(MainProcess, started)> main process start1 相当于导入该模块
windows创造进程相当于导入该文件,由于创建进程在main函数之前,会无限递归导入模块创建进程。
所以在windows创造进程应放在main函数之后,因为main函数不会执行被导入模块。
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\Program Files\Python36\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Program Files\Python36\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Program Files\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\china\PycharmProjects\rq3\并发\课堂\windows_process.py", line 22, in <module>
p.start()
File "C:\Program Files\Python36\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Program Files\Python36\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Program Files\Python36\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
_check_not_importing_main()
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
代码二
import multiprocessing import time import threading print('main process start1') def func(): print('subprocess start',time.asctime(time.localtime(time.time()))) time.sleep(3) print('subprocess end',time.asctime(time.localtime(time.time()))) if __name__ == '__main__': #把进程创建放在main函数中 p = multiprocessing.Process(target=func, name='1号') p.start() print('main process end', time.asctime(time.localtime(time.time())), multiprocessing.current_process()) # 默认所有进程结束后,程序才结束
Windows输出结果
main process start1
main process end Fri Mar 23 11:17:00 2018 <_MainProcess(MainProcess, started)>
main process start1 子进程创建时,导入该模块输出main函数之前语句
subprocess start Fri Mar 23 11:17:00 2018 子进程执行func
subprocess end Fri Mar 23 11:17:03 2018
在Linux上输出结果
main process start1 只输出一个start1
main process end Fri Mar 23 12:29:12 2018 <_MainProcess(MainProcess, started)>
subprocess start Fri Mar 23 12:29:12 2018
subprocess end Fri Mar 23 12:29:15 2018