mandrin
Posts: 65
Joined: Tue Feb 06, 2018 4:49 pm

Petits problèmes avec des threads

Sun Jun 03, 2018 1:25 pm

Bonjour,
Dans le programme ci dessous,
Le petit souci est que lorsque t1 se termine il ne relance pas t0.

Code: Select all

le thread t0 lance t2 et t3 lorsque i0=0
la fin de t0 lance t1 
la fin de t1 relance t0 
t2 et t3 a la fin de leur comptage ils passent en attente

x--t0--x-----t1-------x--t0--x-----t1-------x
x---t2---x            x---t2---x
x------t3------x      x------t3------x

"""
# importations
from tkinter import Tk, Canvas, Button
import threading
import datetime
import time
#import sys
#import RPi.GPIO as GPIO #++

palette=['purple','cyan','maroon','green','red',\
'blue','orange','yellow','dark grey']
global end, i0,i1,i2,i3,T0,T1,T2,T3,date,second_old
end=True
i0=0
i1=50
T0=5
T1=15
T2=7
T3=10
date=datetime.datetime.now() 
second_old=date.second+1 

def wait_for_event0(e0):
#    event_is_set = e0.wait()
    date=datetime.datetime.now() 
    second_old=date.second+1 
    i0=0
    while i0<=T0:
        date=datetime.datetime.now() 
        secs=int(date.second) 
        time.sleep(0.8)
        if i0==T0:
            i1=0
            e1.clear()
            e1.set()
            print('fin de t0',i0)
        if i0==0:
            event_is_set = e1.wait()
            print('début de t0')
            e2.set()
            e3.set()
        if secs==second_old:
            print('secs0->',secs) 
            second_old+=1
            i0+=1
            if second_old==60: 
               second_old=0
            print('i0=>',i0)
        
def wait_for_event1(e1):
    event_is_set = e1.wait()
    date=datetime.datetime.now() 
    second_old=date.second+1 
    i1=0
    print('i1=>',i1)    
    while i1<=T1 :
        date=datetime.datetime.now() 
        secs=int(date.second) 
        time.sleep(0.4)
        if i1==0:
            event_is_set = e0.wait()
            print('i1=>',i1)
            print('début de t1',i1)
        if i1==T1:
            print('fin de t1')
            e0.clear()
            e0.set()
        if secs==second_old:
            print('        secs1->',secs)
            second_old+=1
            i1+=1
            if second_old==60: 
                second_old=0
            print('i1=>',i1)

        
def wait_for_event2(e2):
    event_is_set = e2.wait()
    e2.clear()   
    date=datetime.datetime.now() 
    second_old=date.second+1 
    i2=0
    while i2<=(T2+T0):
        date=datetime.datetime.now() 
        secs=int(date.second) 
        time.sleep(0.4)
        if i2==0:
            print('début de t2')
        if i2==(T2+T0):
            print('fin de t2')
            pass
        if secs==second_old:
            second_old+=1
            i2+=1
            if second_old==60: 
                second_old=0
        
def wait_for_event3(e3):
    event_is_set = e3.wait()
    e3.clear()
    date=datetime.datetime.now() 
    second_old=date.second+1 
    i3=0
    while i3<=(T3+T0):
        date=datetime.datetime.now() 
        secs=int(date.second) 
        time.sleep(0.4)
        if i3==0:
            print('début de t3')
        if i3==(T3+T0):
            print('fin de t3')
            pass
        if secs==second_old:
            second_old+=1
            i3+=1
            if second_old==60: 
                second_old=0
        
# Main code.. 
e0 = threading.Event()
e1 = threading.Event()
e2 = threading.Event()
e3 = threading.Event()

t0 = threading.Thread(name='your_mum', target=wait_for_event0, args=(e0,))
t1 = threading.Thread(name='your_mum', target=wait_for_event1, args=(e1,))
t2 = threading.Thread(name='your_mum', target=wait_for_event2, args=(e2,))
t3 = threading.Thread(name='your_mum', target=wait_for_event3, args=(e3,))

print('Début du programme')
class Menu():

#    c = randrange(8) # génère un nombre aléatoire de 0 à 8
#    couleur = palette [c] # couleur de la ligne
    
    def marche():
        end=True
        t1.start()
        t2.start()
        t3.start()
        t0.start()
        e0.set()
        
    def arret():
        end=False
#        thread1.stop()
#        thread2.stop()
#        threda3.stop()
        
    def menu():
        print('Menu')
        pass

# Création du widget principal ("maître") :
laFenetre = Tk()

# création des widgets "esclaves" :
cannevas = Canvas(laFenetre,bg=palette[1],height=200,width=200)
cannevas.pack(side= "left")

boutonMarche = Button(laFenetre,background='spring green',width=5,height=2, text='Marche', command=Menu.marche)
boutonMarche.pack(padx=5, pady=5)

boutonArret = Button(laFenetre,background='red',width=5,height=2, text='Arrêt', command=Menu.arret)
boutonArret.pack( padx=5, pady=5)

boutonMenu = Button(laFenetre,background='cyan',width=5,height=2, text='Menu', command=Menu.menu)
boutonMenu.pack( padx=5, pady=5)

boutonQuitter = Button(laFenetre,background='light grey',width=5,height=2, text='Quitter', command=laFenetre.quit)
boutonQuitter.pack(side="bottom", padx=5, pady=5)


laFenetre.mainloop () # démarrage du réceptionnaire d'événements

laFenetre.destroy () # destruction (fermeture) de la fenêtre          
 
Si vous voyez une solution je vous en remercie d'avance
Cordialement

Return to “Français”

Who is online

Users browsing this forum: No registered users and 9 guests