Skip to content
Snippets Groups Projects
Commit 7659bf2b authored by Paul Chevalier's avatar Paul Chevalier
Browse files

addition du multithreading

parent e7b70717
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
import time
import threading
# Fonction simulant une tâche lourde (ex: traitement d'image, calculs intensifs)
def tache_lourde():
# Fonction qui effectue une partie du travail
def tache_lourde(part_debut, part_fin, resultat, index):
somme = 0
for _ in range(10**7): # Boucle qui prend un certain temps
for i in range(part_debut, part_fin):
somme += 1
return somme
resultat[index] = somme # Stocker le résultat pour éviter la concurrence
# Paramètres
N = 10**7 # Taille du travail total
THREADS = 4 # Nombre de threads à utiliser
# Exécution sans multithreading
start = time.time()
for _ in range(4): # Exécuter 4 tâches séquentiellement
tache_lourde()
somme_total = sum(range(N)) # Exécution en une seule fois
end = time.time()
print(f"Temps sans multithreading: {end - start:.2f} secondes")
# Exécution avec multithreading
start = time.time()
threads = []
for _ in range(4): # Lancer 4 threads en parallèle
thread = threading.Thread(target=tache_lourde)
resultats = [0] * THREADS # Stocker les résultats des threads
portion = N // THREADS # Taille de la charge par thread
for i in range(THREADS):
debut = i * portion
fin = N if i == THREADS - 1 else (i + 1) * portion
thread = threading.Thread(target=tache_lourde, args=(debut, fin, resultats, i))
threads.append(thread)
thread.start()
for thread in threads:
thread.join() # Attendre que tous les threads finissent
thread.join() # Attendre la fin de tous les threads
somme_total_thread = sum(resultats) # Fusionner les résultats
end = time.time()
print(f"Temps avec multithreading: {end - start:.2f} secondes")
import time
import threading
import os
import sys
from tkinter import Tk, Label
from PIL import Image, ImageTk # Installer Pillow si nécessaire : pip install pillow
# Lancer ton programme principal ici
# --- Création du Splash Screen ---
splash = Tk()
splash.overrideredirect(True) # Enlever la barre de titre
......@@ -22,30 +24,30 @@ splash.geometry(f"{img_width}x{img_height}+{x_pos}+{y_pos}")
# Afficher l'image
label = Label(splash, image=photo)
label.pack()
splash.update() # Mise à jour de l'affichage
# Garder le splash pendant le chargement
splash.update()
# Pour éviter que le programme ne se ferme instantanément
import os
import sys
# --- Fonction pour charger le menu principal ---
def charger_menu():
global splash
base_dir = os.path.dirname(os.path.abspath(__file__))
dossier_path = os.path.join(base_dir, 'Objet')
sys.path.append(dossier_path)
import menu # Importer ici pour éviter les blocages
temps = time.time()
a = menu.App() # Chargement du menu
splash.destroy() # Fermer le splash après le chargement
# Fermer le splash et ouvrir le menu principal
base_dir = os.path.dirname(os.path.abspath(__file__))
tmp = time.time()
print(f"Temps d'exécution : {-(temps - tmp):.6f} secondes")
dossier_path = os.path.join(base_dir,'Objet')
sys.path.append(dossier_path)
a.mainloop() # Lancer l'application principale
a.destroy()
import menu
temps = time.time()
a = menu.App()
splash.destroy()
tmp = time.time()
a.mainloop()
# --- Lancer le chargement en arrière-plan ---
thread = threading.Thread(target=charger_menu)
thread.start()
tot = temps - tmp
print(f"Temps d'exécution : {-tot:.6f} secondes")
\ No newline at end of file
# --- Affichage du splash screen pendant le chargement ---
splash.mainloop()
......@@ -43,15 +43,15 @@ def load_main_program():
base_dir = os.path.dirname(os.path.abspath(__file__))
dossier_path = os.path.join(base_dir,'Code_objets')
dossier_path = os.path.join(base_dir,'Objet')
sys.path.append(dossier_path)
import Menu
import menu
# Lancer le programme principal ici
a = Menu.App()
a = menu.App()
a.mainloop()
# Démarrer le splash screen dans un thread
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment