参考链接: 【莫烦Python】Multiprocessing 让你的多核计算机发挥真正潜力 Python
参考链接: 莫烦多进程学习网站
参考链接: multiprocessing — 基于进程的并行
test01.py
# 第2集
import multiprocessing as mp
import multiprocessing
# import threading as td
# 注意使用多进程必须要用到
# if __name__ == '__main__':
# 否则会报错 RuntimeError
# 多进程与多线程用法极其相似
def job(a,b):
print('aaa')
# t1 = td.Thread(target=job,args=(1,2))
# t1.start()
# t1.join()
if __name__ == '__main__':
p1 = multiprocessing.Process(target=job,args=(1,2)) # 进程对象表示在单独进程中运行的活动。
p1.start()
# 启动进程活动。每个进程对象最多只能调用一次。
# 它安排对象的 run() 方法在一个单独的进程中调用。
p1.join()
# 如果可选参数 timeout 是 None (默认值),
# 则该方法将阻塞,直到调用 join() 方法的进程终止。
print('END')
r"""
File "D:\Anaconda3\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:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
"""
控制台下输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 1052 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多进程> & 'D:\Anaconda3\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '64188' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多进程\test01.py'
aaa
END
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多进程> conda activate base
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多进程>
注意,在python IDLE下运行不会有效果:
可以在win10下的cmd窗口下运行:
注意,要使用 if __name__ == '__main__':语句,否则报错:
# 第2集
import multiprocessing as mp
import multiprocessing
# import threading as td
# 注意使用多进程必须要用到
# if __name__ == '__main__':
# 否则会报错 RuntimeError
# 多进程与多线程用法极其相似
def job(a,b):
print('aaa')
# t1 = td.Thread(target=job,args=(1,2))
# t1.start()
# t1.join()
# if __name__ == '__main__':
# p1 = multiprocessing.Process(target=job,args=(1,2)) # 进程对象表示在单独进程中运行的活动。
# p1.start()
# # 启动进程活动。每个进程对象最多只能调用一次。
# # 它安排对象的 run() 方法在一个单独的进程中调用。
# p1.join()
# # 如果可选参数 timeout 是 None (默认值),
# # 则该方法将阻塞,直到调用 join() 方法的进程终止。
# print('END')
p1 = multiprocessing.Process(target=job,args=(1,2)) # 进程对象表示在单独进程中运行的活动。
p1.start()
# 启动进程活动。每个进程对象最多只能调用一次。
# 它安排对象的 run() 方法在一个单独的进程中调用。
p1.join()
# 如果可选参数 timeout 是 None (默认值),
# 则该方法将阻塞,直到调用 join() 方法的进程终止。
print('END')
r"""
File "D:\Anaconda3\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:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
"""
报错信息如下:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 822 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多进程> conda activate base
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多进程> & 'D:\Anaconda3\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '64358' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\测试多进程\test01.py'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "D:\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "D:\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "D:\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "D:\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "D:\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "D:\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "D:\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\chenxuqi\Desktop\News4cxq\测试多进程\test01.py", line 31, in <module>
p1.start()
File "D:\Anaconda3\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
File "D:\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "D:\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "D:\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "D:\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
_check_not_importing_main()
File "D:\Anaconda3\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:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
END
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\测试多进程>