Bonjour,

Après pas mal de difficultées j'ai enfin un début de progamme qui quitte proprement mes threads.

Avant de les étoffer j'aimerais avoir votre avis afin d'optimiser ce petit programme qui me paraît un peu touffu.

code appelé AffThread4 :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
import threading
import datetime
import random
import time
import sys
import RPi.GPIO as GPIO #++
 
end=''
 
GPIO.setmode(GPIO.BCM) #++ GPIO21= broche 40
GPIO.setup(20, GPIO.OUT, initial=GPIO.LOW)# broche 38 LED #++
 
class Afficheur(threading.Thread):
 
    """Thread chargé simplement d'afficher une lettre dans la console."""
 
    def __init__(self, lettre):
        threading.Thread.__init__(self)
        self.lettre = lettre
        self.end = True
 
    def run(self):
        """Code à exécuter pendant l'exécution du thread."""
        i = 0
        print('end2=',self.end)
        while (i < 20) & self.end == True:
#            sys.stdout.write(self.lettre)
#            sys.stdout.flush() # affichage immediat
            print(self.lettre)
            attente = 0.2
            attente += random.randint(1, 60) / 100 # attente aleatoire de 0.2 a 0.8 s
            time.sleep(attente)
            i += 1
            print('end2 ',self.end)
            if self.end  == False:
                break
 
    def stop(self):
        print("Fin Afficheur !")
        self.end = False
# fin de Afficheur
 
class Compteur(threading.Thread):
    """ Fait clignoter une led """
    def __init__(self):
        threading.Thread.__init__(self)
        self.end = True
 
    def run(self):
        i = 0
        date=datetime.datetime.now() #*-*
        second_old=date.second+1 #*-*
        while self.end == True:
            try:
                date=datetime.datetime.now() #*-*
                secs=int(date.second) #*-*
                Led=date.microsecond/1E6
    # commande de la LED toute les secondes (0.5s allumee, 0.5s eteinte)
                if  Led>=0.5:
                    GPIO.output(20,GPIO.LOW)# LED  allumee #++
                    pass # equiv a nop
                else:
                    GPIO.output(20,GPIO.HIGH)# LED eteinte #++
                    pass # equiv a nop                
                if secs==second_old:
                    i+=1
                    print('secs->',secs) #*/*
                    second_old+=1  
                    if second_old==60: 
                        second_old=0
            except RuntimeError:
                self.end = False
                GPIO.cleanup()
 
    def stop(self):
        print("Fin Compteur !")
        self.end = False
code appelant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
import time
import AffThread4
 
end=True
thread1=AffThread4.Compteur()
thread2 =AffThread4.Afficheur('A')
print("Entrez M pour marche")
print("  ou   A  pour arrêt   ")
Fin=1
while Fin:
    try:
        choix=input("Choix :")
        time.sleep(0.1)
        choix=choix.upper()
        print('choisi->',choix)
        if choix=='M':
            end=True
            thread1.start()
            thread2.start()
        if choix== 'A':
            end=False
            thread1.stop()
            thread2.stop()
            Fin=0
    except KeyboardInterrupt:
        thread1.join()
        thread2.join()
 
if Fin==0:
    thread1.join()
    thread2.join()
Vos avis me seraient très utiles
Merci d'avance
Cordialement