import logging import sys import time # --- Configuration --- NUM_TASKS = 15 SLEEP_DURATION = 0.2 # Chaque tâche "attendra" 0.2 seconde # --- Fonctions --- def setup_logging(): """Configure un logging simple pour la sortie console.""" logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", handlers=[logging.StreamHandler(sys.stdout)], ) def worker_task(task_id: int): """ Une fonction qui simule un travail bloquant (ex: une longue requête réseau). time.sleep() gèle le thread et bloque l'exécution de tout autre code. """ logging.info(f"Tâche {task_id}: Démarrage...") time.sleep(SLEEP_DURATION) logging.info(f"Tâche {task_id}: Terminée.") return f"Résultat de la tâche {task_id}" def main(): """Fonction principale du script synchrone.""" setup_logging() logging.info("--- DÉMARRAGE DE LA SIMULATION SYNCHRONE ---") start_time = time.perf_counter() # Les tâches sont exécutées l'une après l'autre. # La suivante ne commence que lorsque la précédente est terminée. results = [worker_task(i) for i in range(NUM_TASKS)] duration = time.perf_counter() - start_time logging.info("-" * 50) logging.info(f"Exécution SYNCHRONE terminée en {duration:.2f} secondes.") expected_time = NUM_TASKS * SLEEP_DURATION logging.info(f"Temps attendu : {NUM_TASKS} tâches * {SLEEP_DURATION}s = {expected_time:.2f}s.") logging.info("Les temps d'attente se sont additionnés.") logging.info("-" * 50) if __name__ == "__main__": main()