Thread pausieren / neustarten?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
mayx
User
Beiträge: 71
Registriert: Sonntag 3. Mai 2009, 02:51

Cool, es läuft ;)

Leider steht in der Api:
"The interval the timer will wait before executing its action may not be exactly the same as the interval specified by the user."

Welche ist denn die genauste Möglichkeit Zeitintervalle abzugeben?

Code: Alles auswählen

#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-

import threading
import time

class Loop(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        print 'Start Init'
        self.waitTime = 1
        self.t = threading.Timer(self.waitTime, self.loopIt)
   
    def start(self):
        print 'Start Code'
        self.t.start() 
        
    def startItAgain(self):
        print 'Start startItAgain'
        self.t = threading.Timer(self.waitTime, self.loopIt)
        self.t.start()     
    
    def loopIt(self):
        print 'loopIt'
        self.t = threading.Timer(self.waitTime, self.loopIt)
        self.t.start() 
        
    def stopIt(self):
        print 'stopIt'
        self.t.cancel()

    
class Application:
    def __init__(self):
        print "Main- Create and start counter"         
        loop = Loop()
        loop.start()
        time.sleep(5)
        print "Main - Stop counter"
        loop.stopIt()
        print "Main - Still running 1"
        print "Main - Still running 2"
        time.sleep(5)
        print "Main - Start counter again"
        loop.startItAgain()     
        time.sleep(5)
        print "Stop counter"
        loop.stopIt()
        print "Main - Program finished."
        
        
if __name__ == "__main__":
   app = Application()
oder:

Code: Alles auswählen

#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-

import threading
import time


class Loop(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.count = 0
        # Values for self.running:
        # 0 : Ready.
        # 1 : Running.
        # 2 : Finished.
        self.running = 0

    def run(self):
        self.running = 1
        self.loop()

    def loop(self):
        pass
        print 'loop'
        time.sleep(0.1)
        if not self.running == 2:
            self.loop()

    
class Application:
    def __init__(self):
        print "Create and start counter"         
        loop = Loop()
        loop.start()
        print "Main-Programm don«t need to wait"   
        time.sleep(1)
        print "Main-Programm is still running - great"
        time.sleep(1) 
        print "Stop counter"
        loop.running = 2
        time.sleep(1)
        print "Start counter again"
     #   del loop
        loop = Loop()
        loop.start()
        time.sleep(2)
        print "Main-Programm should«t wait" 
        loop.running = 2  
        print "Main-Program finished."



Vielen Dank euch Alle!
alpha
User
Beiträge: 195
Registriert: Freitag 23. Mai 2003, 23:24
Wohnort: Ulm

Hallo Mayx,

es funktioniert vielleicht, aber meiner Meinung nach nicht sehr lange!

Du rufst loop mit self.loop rekursiv auf, dabei wird loop aber nie fertig.
Ich denke irgendwann bekomst nen Stackoverflow.

Bitte schau Dir http://www.python-forum.de/topic-3869.html
an.. Da zeigt dir Gerold wie man es richtig macht :D

Grüße
alpha
mayx
User
Beiträge: 71
Registriert: Sonntag 3. Mai 2009, 02:51

ah, super.
danke.
Antworten