线程和功能'打印'

时间:2014-11-21 20:58:52

标签: python-3.x thread-synchronization

我试图并行化一个脚本,该脚本打印出目录中有多少文档,图片和视频以及其他一些信息。我已将串行脚本放在此消息的末尾。这是一个示例,显示了如何输出有关给定目录的信息:

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()

当我跑步时,没有打印出来!当我输入&#39; +&#39;返回键&#39;在解释器中,我得到<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  

1 个答案:

答案 0 :(得分:0)

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

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

for t in types:
    t.__run__()

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