import os,time,random

from  multiprocessing  import Process

PROCESS_MAX_NUM = 10

def get_num(number):
    print(number)
    time.sleep(10*random.random())



if __name__ == "__main__":    
    process_dict = {}
    for number in range(100):
        if len( process_dict.keys() ) < PROCESS_MAX_NUM:
            p =  Process( target=get_num, args=(number ,) )        
            p.start()
            process_dict[ p ]  =  time.time()
            continue

        while len( process_dict.keys() ) >= PROCESS_MAX_NUM :
            for p in list( process_dict.keys() ):
                current_time = time.time()
                if ( current_time - process_dict[p] )  > 5  and  p.is_alive():
                    print(p.name + " is terminate ")
                    p.terminate()
                    del process_dict[p]
                elif not p.is_alive():
                    del process_dict[p]
                else:
                    continue                
            time.sleep(1)

    if len( process_dict.keys() ) == 0:
        print("all task done")     

上面的程序运行时总是不会输出最后的打印信息,而且时不时的少数据,应该是打印出来 100 个网址的,但是实际上只打印出来了 60 多个数

请高手赐教,为啥会这样?


↙↙↙阅读原文可查看相关链接,并与作者交流