Code source de ensae_teaching_cs.td_2a.parallel_thread

# -*- coding: utf-8 -*-
"""
Ce fichier contient un exemple qui permet d'exécuter plusieurs threads.


:githublink:`%|py|5`
"""

import threading
import time


[docs]class ParallelThread(threading.Thread): """ Cette classe implémente un thread qui exécute en boucle une fonction sur tous les éléments d'une liste. :githublink:`%|py|15` """
[docs] def __init__(self, f, list_of_params): """ Constructeur :param f: fonction à exécuter :param list_of_params: liste des paramètres à exécuter :githublink:`%|py|23` """ threading.Thread.__init__(self) self.f2run = f self.results = None self.list_of_params = list_of_params
[docs] def run(self): """ Appelle une fonction plusieurs sur tous les paramètres dans une liste. :githublink:`%|py|32` """ self.results = [] for params in self.list_of_params: l, p = (params, {}) if len(params) else params self.results.append(self.f2run(*l, **p))
[docs] @staticmethod def parallel(f, list_of_params, nbthread=2, wait=True, daemon=True, delay_sec=1): """ Parallélise l'appel à la fonction ``f`` sur une liste de paramètres. :param f: fonction à appeler sur chacun des paramètres de la liste :param list_of_params: liste des paramètres :param nbthread: nombre de threads :param wait: attendre pour la fin :param daemon: voir `daemon <https://docs.python.org/3/library/threading.html#threading.Thread.daemon>`_ :param delay_sec: la fonction inclut une boucle qui attend les threads, elle vérifie cela toutes ``delay_sec`` secondes :githublink:`%|py|51` """ th = [] split = [list_of_params[i::nbthread] for i in range(nbthread)] for spl in split: t = ParallelThread(f, spl) t.daemon = daemon th.append(t) t.start() if wait: waits = th.copy() while len(waits) > 0: waits = [t_ for t_ in th if t_.is_alive()] if len(waits) > 0: time.sleep(delay_sec) final = [None for i in range(len(list_of_params))] for i in range(nbthread): final[i::nbthread] = th[i].results return final return th