Bonjour
est-ce que time.sleep(-1) lance un process qui ne se termine jamais? j'ai besoin d'un thread qui attend un temps variable, ou n'attend pas et ne fais rien, je voulais mettre alors des temps negatifs pour ça mais ça n'a pas l'air interressant

je voudrais pouvoir changer ce temps et en le mettant negatif par exemple le thread se met en pause

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
import threading
import time
 
class timeout(threading.Thread):
    def __init__(self, rate = 60):
        threading.Thread.__init__(self)
        self.rate = rate
        self.stopped=False
    def run(self):
        while self.rate>0:
            print self.rate, time.time()
            time.sleep(self.rate)
        self.stopped=True
        print "timeout end"
    def setrate(self, rate):
        self.rate = rate 
        if self.stopped:
            print "timeout restart"
            self.start()  
 
class changer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        time.sleep(10)
        global tout
        tout.setrate(2)
        time.sleep(10)
        tout.setrate(-1)
        time.sleep(10)
        tout.setrate(5)
        print "changer" 
 
 
global tout       
tout=timeout(5)
tout.start()
changer().start()
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
5 1290776845.79
5 1290776850.8
5 1290776855.8
2 1290776860.8
2 1290776862.8
2 1290776864.81
timeout end
timeout restart
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "test.py", line 31, in run
    tout.setrate(5)
  File "test.py", line 19, in setrate
    self.start()
  File "/usr/lib/python2.6/threading.py", line 467, in start
    raise RuntimeError("thread already started")
RuntimeError: thread already started