python windows上调用subprogress


#由于windows不支持os.wait(),系统会不停的创建进程, 这里使用一个sets()作为一个池,控制进程的数量,使用subprogress 调用外部命令完成工作


#-*- coding:utf-8-*-


import os

import subprocess

import time


pids = []

n = 0


ps = set()

max_p = 20


for i in open("imgurlok.txt","r").readlines():

(num,name,url) = i.split("|||")

dirname = "f:/ooo/img5/"+str(num)+"/"

if not os.path.exists(dirname):

os.mkdir(dirname)

cmd = "wget -t3 -T5 " + " -P "+ dirname.encode("gbk","ignore") + " "+url+" > nul"

p = subprocess.Popen(cmd,stdin=None,stdout=None, shell=True)

ps.add(p)

while len(ps) >= max_p:

time.sleep(0.001)#这个值如果设置得当,可以不再下面进行异常捕获的操作

try:

ps.difference_update(d for d in ps if d.poll() is not None)

except:

pass