foudesbois
9/21/2017 - 9:08 AM

Thread Python by class

#!/usr/bin/env python
# coding: utf-8
import threading
import time
from __future__ import print_function

class MyThread(threading.Thread):
    def run(self):
        print("{} started!".format(self.getName()))           # affiche "Thread-x started!"
        time.sleep(1)                                         # simule un travail d'une seconde
        print("{} finished!".format(self.getName()))          # affiche "Thread-x finished!"

if __name__ == '__main__':
    for x in range(4):                                        # Quatre fois...
        mythread = MyThread(name = "Thread-{}".format(x + 1)) # ...Instancie un thread et lui donne un ID unique
        mythread.start()                                      # ...Démarre le thread
        time.sleep(.9)                                        # ...Attend 0,9 secondes avant d'en démarrer un autre