Last active
May 16, 2020 17:31
-
-
Save Franck1333/a6b0270080227fd115f01654a45c5bf7 to your computer and use it in GitHub Desktop.
Multi-Threading #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| #https://openclassrooms.com/fr/courses/235344-apprenez-a-programmer-en-python/2235545-faites-de-la-programmation-parallele-avec-threading | |
| import random | |
| import sys | |
| from threading import Thread | |
| import time | |
| class Afficheur(Thread): | |
| """Thread chargé simplement d'afficher une lettre dans la console.""" | |
| def __init__(self, lettre): | |
| Thread.__init__(self) | |
| self.lettre = lettre | |
| def run(self): | |
| """Code à exécuter pendant l'exécution du thread.""" | |
| i = 0 | |
| while i < 20: | |
| sys.stdout.write(self.lettre) | |
| sys.stdout.flush() | |
| attente = 0.2 | |
| attente += random.randint(1, 60) / 100 | |
| time.sleep(attente) | |
| i += 1 | |
| # Création des threads | |
| thread_1 = Afficheur("1") | |
| thread_2 = Afficheur("2") | |
| thread_7 = Afficheur("7") | |
| # Lancement des threads | |
| thread_1.start() | |
| thread_2.start() | |
| thread_7.start() | |
| # Attend que les threads se terminent | |
| thread_1.join() | |
| thread_2.join() | |
| thread_7.join() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| #https://dzone.com/articles/python-thread-part-1 | |
| #https://www.shanelynn.ie/asynchronous-updates-to-a-webpage-with-flask-and-socket-io/ | |
| import time | |
| import threading #import Thread | |
| def func1(): | |
| print (' first func running') | |
| time.sleep(1) | |
| print (' first func done') | |
| def func2(): | |
| print (' second func running') | |
| time.sleep(1) | |
| print (' second func done') | |
| threadFunc1 = threading.Thread(target=func1) | |
| threadFunc1.start() | |
| threadFunc2 = threading.Thread(target=func2) | |
| threadFunc2.start() | |
| threadFunc1.join() | |
| threadFunc2.join() | |
| if not threadFunc1.is_alive(): | |
| print("threadFunc1 not Alive (working)") | |
| #why not re-start the thread so ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment