English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans les sciences informatiques, un thread est un ensemble d'instructions qui peut être géré indépendamment par le planificateur, et le planificateur fait partie du système d'exploitation.
La principale fonction des threads est d'exécuter plusieurs threads en même temps. Un thread signifie différentes tâches, les appels de fonctions dans le programme et l'exécution simultanée de plusieurs threads, ce qui ne signifie pas qu'ils s'exécutent sur des ordinateurs différents.
Dans ces deux cas, on utilise des threads multiples.
Lorsque les sorties des sous-programmes doivent être combinées avec le programme principal.
Lorsque le programme principal contient des codes indépendants les uns des autres.
Python fournit un module de threads très puissant et offre également un soutien avancé pour les threads.
Le module threading définit de nombreuses fonctions, qui sont utilisées pour obtenir des données liées aux threads et qui s'exécutent automatiquement.
threading.active_count()
Cette fonction retourne le nombre d'objets Thread actifs. Ici, le comptage retourné est égal à la longueur de la liste retournée.enumerate()
.
threading.current_thread()
Cette fonction retourne l'objet Thread courant, qui correspond au thread de contrôle de l'appelant.
threading.get_ident()
Cette fonction retourne l'"identifiant de thread" actuel du thread courant. Il s'agit d'un entier non nul.
threading.enumerate()
This function returns a list of all currently active Thread objects (including daemon threads). The current_thread() function creates virtual threads and the main thread, and excludes terminated threads and threads that have not yet started.
threading.main_thread()
This function returns the main thread object.
threading.settrace(func)
After starting all threads from the thread module, set the trace function. Inrun()
Before calling this function, it will pass each thread to sys.settrace().
threading.setprofile(func)
After starting all threads from the thread module, set the configuration file function. Inrun()
Before calling this function, it will pass each thread to sys.setprofile().
threading.stack_size([size])
This function returns the size of the thread stack and is used when creating a new thread.
Thread.TIMEOUT_MAX
This is a constant that has the maximum value of the timeout parameter for blocking functions (Lock.acquire(), RLock.acquire(), Condition.wait(), etc.).
import threading def trace_function(): print("Passing the trace function") def profile(): print("PROFILE THREAD: " + str(threading.current_thread().getName())) class mythread(threading.Thread): def __init__(self, thread_name, thread_ID): threading.Thread.__init__(self) self.thread_name = thread_name self.thread_ID = thread_ID def run(self): print(str(self.thread_ID)); print("ACTIVE THREADS ARE: "+ str(threading.active_count())) print("CURRENT THREAD IS: " + str(threading.current_thread().getName())) my_thread1 mythread("PP", 500) my_thread2 mythread("PythonProgram", 1000); print("NAME OF THE MAIN THREAD: ") + str(threading.main_thread().getName())) print("IDENTIFICATION OF MAIN THREAD: ")+ str(threading.get_ident())) print("TAILLE DU STACK = ") + str(threading.stack_size())) print(threading.settrace(trace_function())) threading.setprofile(profile()) my_thread1.start() my_thread2.start() print("LIST OF ENUMERATION: ") print(threading.enumerate()) print("SORTIE")
Résultat de la sortie
NAME OF THE MAIN THREAD: MainThread IDENTIFICATION OF MAIN THREAD: 5436 TAILLE DU STACK = 0 Passing the trace function None PROFIL DE THREAD: MainThread 500 1000LIST OF ENUMERATION: ACTIVE THREADS ARE: 6 [<_MainThread(MainThread, started 5436)>, <Thread(Thread-4, started daemon 1960)>, <Heartbeat(Thread-5, started daemon 6452)>, <HistorySavingThread(IPythonHistorySavingThread, started 4304)>, <mythread(Thread-8, started 8460)>, <mythread(Thread-9, started 4668)>] SORTIE CURRENT THREAD IS: Thread-8 ACTIVE THREADS ARE: 5 CURRENT THREAD IS: Thread-9