Bonsoir à tous,
J'ai écrit un script qui retourne quelques informations caractérisant un répertoire (nombres de documents, images et vidéos qui existent, et la taille qu'ils occupent) dont le chemin absolue est connue. Je voudrai le paralléliser afin de gagner du temps à l'exécution.
Voici le code séquentiel :
et un exemple de ce qu'il affiche :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 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
J'ai essayé de paralléliser le code avec des threads. Voilà ce que ça donne :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 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)
Mais lorsque j'exécute ce code, rien ne s'affiche malgré le print que j'ai mis entre mon verrou cv. En plus, lorsque je tape 't' et que je clique sur la touche 'Entrée', on me retourne
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 import tools import time import os import os.path import hashlib import threading directory_path="Users/usersos/Desktop/j" verrou=threading.Lock() type_=["documents","pictures","videos"] e={} e["documents"]=[".pdf",".html",".rtf",".txt"] e["pictures"]=[".png",".jpg",".jpeg"] e["videos"]=[".mpg",".avi",".mp4",".mov"] global n global s global p class type_thread(threading.Thread): #for documents, pictures, and videos def __init__(self,n,e_): super().__init__() self.extensions=e_ self.name=n def __run__(self): files=tools.g(directory_path,self.extensions) cv.acquire() n=len(files) s=tools.size1(files) p=s*100/tools.size2(directory_path) 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().
Code : Sélectionner tout - Visualiser dans une fenêtre à part <type_thread(videos, stopped 4367323136)>
Qu'est ce qui empêche le logiciel d'écrire ce que je lui demande d'écrire ? On dirait que __run__() n'est pas lancée avec start() ….
Bien cordialement.
Partager