用于多进程的Python
此脚本从txt文件中读取行,并用作我的外部命令的一部分:用于多进程的Python,python,python-3.x,performance,optimization,multiprocessing,Python,Python 3.x,Performance,Optimization,Multiprocessing,此脚本从txt文件中读取行,并用作我的外部命令的一部分: 用于txt do中的每一行 操作系统(comand) 是否可以使用多进程来提高速度? i=0 x=[] 对于os.listdir(a)中的文件: 如果文件.endswith('.json'): x、 附加(文件) i+=1 打印('文件总数:'+str(i)) archivo='/temp.txt' strm=open(archivo,encoding='UTF8') 最小值=1 对于strm中的行: 如果min>i: 最小值=1 or
用于txt do中的每一行
操作系统(comand)
是否可以使用多进程来提高速度?
i=0
x=[]
对于os.listdir(a)中的文件:
如果文件.endswith('.json'):
x、 附加(文件)
i+=1
打印('文件总数:'+str(i))
archivo='/temp.txt'
strm=open(archivo,encoding='UTF8')
最小值=1
对于strm中的行:
如果min>i:
最小值=1
origen=args.o+“/”+line.rstrip('\n')
destino=re.sub(“\/(?:(?!\/)+$”,”,args.d+“/”+line.rstrip('\n'))
comando=args.r+“复制\”+origen+“\”\“\”+destino+“\”--驱动器服务帐户文件\+地毯+“\\”+str(最小值)+“.json\”--仅限大小”
操作系统(comando)
最小+=1
您可以使用多处理
从命令列表启动操作系统进程
大概是这样的:
import multiprocessing, os, re
from multiprocessing import Process, freeze_support
def getcmdlist():
i=0
x=[]
for file in os.listdir(carpeta):
if file.endswith('.json'):
x.append(file)
i+=1
print('the total number of files: ' +str(i))
archivo='./temp.txt'
strm=open(archivo,encoding='UTF8')
min=1
cmdlist = []
for line in strm:
if min > i:
min=1
origen=args.o+"/"+line.rstrip('\n')
destino=re.sub("\/(?:.(?!\/))+$","",args.d+"/"+line.rstrip('\n'))
comando=args.r+" copy \""+origen+"\" \""+destino+"\" --drive-service-account-file \""+carpeta+"\\"+str(min)+".json\" --size-only"
cmdlist.append(comando)
min+=1
return cmdlist
if __name__ == '__main__':
freeze_support() # needed for Windows
cmdlist = getcmdlist() # all commands
with multiprocessing.Pool(processes=4) as pool:
pool.map(os.system, cmdlist) # pass each command to os process
我在使用多处理时遇到此错误。池(进程=4)作为池:名称错误:未定义名称“多处理”。请在脚本顶部添加导入。如果需要,安装模块:正常工作。谢谢。我可以放更多的进程吗?由于操作系统消耗的资源很少,限制是什么?理论上,进程计数没有限制。这只取决于您希望一起运行的进程数和计算机的功率。如果未指定进程计数,将使用CPU计数。