2014-11-21 77 views
1

我想并行化一个脚本,打印出目录中有多少文档,图片和视频以及其他一些信息。我已将串行脚本放在此消息的末尾。这里有一个例子来展示它是如何对输出给定目录中的信息:现在线程和函数'打印'

7 documents use 110.4 kb (  1.55 % of total size) 
2 pictures use 6.8 Mb ( 98.07 % of total size) 
0 videos use 0.0 bytes (  0.00 % of total size) 
9 others use 26.8 kb (  0.38 % of total size) 

,我想使用线程来尽量减少执行时间。我试过这个:

import threading 
import tools 
import time 
import os 
import os.path 

directory_path="Users/usersos/Desktop/j" 
cv=threading.Lock() 

type_=["documents","pictures","videos"] 
e={} 
e["documents"]=[".pdf",".html",".rtf",".txt"] 
e["pictures"]=[".png",".jpg",".jpeg"] 
e["videos"]=[".mpg",".avi",".mp4",".mov"] 


class type_thread(threading.Thread): 
    def __init__(self,n,e_): 
     super().__init__() 
     self.extensions=e_ 
     self.name=n 
    def __run__(self): 
     files=tools.g(directory_path,self.extensions) 
     n=len(files) 
     s=tools.size1(files) 
     p=s*100/tools.size2(directory_path) 
     cv.acquire() 
     print("{} {} use {} ({:10.2f} % of total size)".format(n,self.name,tools.compact(s),p)) 
     cv.release() 


types=[type_thread(t,e[t]) for t in type_] 
for t in types: 
    t.start() 
for t in types: 
    t.join() 

当我运行,没有打印出来!当我在解释器中键入't'+'返回键'时,我得到<type_thread(videos, stopped 4367323136)>更有甚者,解释器有时会用这些相同的键返回正确的统计数据。

这是为什么?


初始脚本(串行):

import tools 
import time 
import os 
import os.path 

type_=["documents","pictures","videos"] 
all_=type_+["others"] 
e={} 
e["documents"]=[".pdf",".html",".rtf",".txt"] 
e["pictures"]=[".png",".jpg",".jpeg"] 
e["videos"]=[".mpg",".avi",".mp4",".mov"] 

def statistic(directory_path): 

    #----------------------------- Computing --------------------------------- 

    d={t:tools.g(directory_path,e[t]) for t in type_} 
    d["others"]=[os.path.join(root,f) for root, _, files_names in os.walk(directory_path) for f in files_names if os.path.splitext(f)[1].lower() not in e["documents"]+e["pictures"]+e["videos"]] 
    n={t:len(d[t]) for t in type_} 
    n["others"]=len(d["others"]) 
    s={t:tools.size1(d[t]) for t in type_} 
    s["others"]=tools.size1(d["others"]) 
    s_dir=tools.size2(directory_path) 
    p={t:s[t]*100/s_dir for t in type_} 
    p["others"]=s["others"]*100/s_dir 

    #----------------------------- Printing --------------------------------- 

    for t in all_: 
     print("{} {} use {} ({:10.2f} % of total size)".format(n[t],t,tools.compact(s[t]),p[t])) 
    return s_dir 

回答

0

方法start()方法似乎不起作用。当我更换

for t in types: 
    t.start() 
for t in types: 
    t.join() 

for t in types: 
    t.__run__() 

它工作正常(至少现在,我不知道这是否会仍然当我将添加其他命令)。