5.获取进程编号
示例:
获取当前进程pid
import time
import multiprocessing #导入进程包
import os
# 唱歌
def sing(num,name):
print("sing process pid",os.getpid())
for i in range(num):
print(name)
print("唱歌....")
time.sleep(0.5)
def dance(nums,name):
print("dance process pid", os.getpid())
for i in range(nums):
print(name)
print("跳舞....")
time.sleep(0.5)
if __name__ == '__main__':
sing_process = multiprocessing.Process(target= sing,args=(3,"小明"))
dance_process = multiprocessing.Process(target=dance,kwargs={"nums": 4,"name":"小明"})
sing_process.start()
dance_process.start()
"G:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe" G:/PythonCode/PY/processwithparameters.py
dance process pid 15512
小明
跳舞....
sing process pid 20220
小明
唱歌....
小明
跳舞....
小明
唱歌....
小明
跳舞....
小明
唱歌....
小明
跳舞....
Process finished with exit code 0
获取父进程pid
import time
import multiprocessing #导入进程包
import os
# 唱歌
def sing(num,name):
print("sing process pid",os.getpid())
print("唱歌进程的父进程 pid", os.getppid())
for i in range(num):
print(name)
print("唱歌....")
time.sleep(0.5)
def dance(nums,name):
print("dance process pid", os.getpid())
print("跳舞进程的父进程 pid", os.getppid())
for i in range(nums):
print(name)
print("跳舞....")
time.sleep(0.5)
#主进程
if __name__ == '__main__':
#创建子进程并指定执行的任务名
print("主进程的pid:",os.getpid())
sing_process = multiprocessing.Process(target= sing,args=(3,"小明"))
dance_process = multiprocessing.Process(target=dance,kwargs={"nums": 4,"name":"小明"})
#启动子进程并执行任务
sing_process.start()
dance_process.start()
"G:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe" G:/PythonCode/PY/processwithparameters.py
主进程的pid: 16472
dance process pid 13132
sing process pid 16108
跳舞进程的父进程 pid 16472
小明
跳舞....
唱歌进程的父进程 pid 16472
小明
唱歌....
小明
跳舞....
小明
唱歌....
小明
跳舞....
小明
唱歌....
小明
跳舞....
Process finished with exit code 0
6.进程的注意点
主进程等待子进程结束
import time
import multiprocessing
def work():
for i in range(10):
#共延时2s
print("work ...")
time.sleep(0.2)
if __name__ == '__main__':
work_process = multiprocessing.Process(target= work)
work_process.start()
time.sleep(1)
print("主进程完成")
"G:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe" G:/PythonCode/PY/Process_Attention_Points.py
work ...
work ...
work ...
work ...
work ...
主进程完成
work ...
work ...
work ...
work ...
work ...
Process finished with exit code 0
import time
import multiprocessing
def work():
for i in range(10):
#共延时2s
print("work ...")
time.sleep(0.2)
if __name__ == '__main__':
work_process = multiprocessing.Process(target= work)
work_process.daemon = True
work_process.start()
time.sleep(1)
print("主进程完成")
"G:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe" G:/PythonCode/PY/Process_Attention_Points.py
work ...
work ...
work ...
work ...
work ...
主进程完成
Process finished with exit code 0
7.视频文件夹高并发copy器
示例:
import os
import multiprocessing
def Copyfile(file_name,source_dir,dest_dir):
#拼接源文件路径和目标文件路径
source_path = source_dir +"/" + file_name
dest_path = dest_dir + "/" +file_name
#打开源文件和目标文件
with open(source_path,"rb") as source_file:
with open(dest_path,"wb") as dest_file :
#循环读取源文件到目标路径
while True:
#读前k个文件
data = source_file.read(1024)
#数据存在
if data :
dest_file.write(data)
else:
break
if __name__ =='__main__':
source_dir = "H:/RockV2/0"
dest_dir = "G:/PythonCode/Multipleprocesses_Learn"
#如果文件夹不存在则创建文件夹
try:
os.mkdir(dest_dir)
except:
print("目标文件夹已存在")
#获取给定目录下的所有文件名
file_list = os.listdir(source_dir)
#遍历文件名实现拷贝
for file_name in file_list:
#使用多进程实现多任务拷贝
sub_process = multiprocessing.Process(target= Copyfile,args=(file_name,source_dir,dest_dir))
sub_process.start()