Salut,
Je n'arrive pas à arrêter un thread en utilisant atexit.register Peut-être n'est-ce pas possible? peut être ai-je raté quelque chose? J'ai remarqué que quand je commente la ligne task.open(), la fonction quitApp s'exécute bien à la sortie, alors que si les threads sont actifs elle ne s'exécute pas.
A+
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 import threading import sys import time import atexit class DUMMY_THREAD(): def __init__(self, text='.', period=1.0): self.__running = False self.__period = float(period) self.__text = text self.__thread = None self.__id = "DUMMY_THREAD(\"%s\")"%self.__text def __str__(self): return self.__id def open(self, *args, **kwds): if not self.__running: self.__running = True self.__thread = threading.Thread(None, self.task, self.__id) sys.stdout.write("%s open!\n"%str(self.__thread)) self.__thread.start() def close(self): if self.__running: sys.stdout.write("%s closed!\n"%str(self.__thread)) self.__running = False def task(self): sys.stdout.write("%s started!\n"%str(self.__thread)) target = time.time() + self.__period while self.__running: if time.time() < target: time.sleep(0.001) else: target += self.__period sys.stdout.write(self.__text) sys.stdout.write("%s terminated!\n"%str(self.__thread)) if __name__ == "__main__": import Tkinter as tk def quitApp(*args): print "quitApp %s"%" ".join(args) texts = ["-", "+", "*", "/", "\n"] for index in range(len(texts)): task = DUMMY_THREAD(texts[index]) task.open() atexit.register(task.close) atexit.register(lambda:quitApp("azerty", "qwerty")) raw_input("hit RETURN to stop threads") print "should stop..."
Pfeuh
Partager