Messages : 39
Sujets : 6
Inscription : Apr 2015
bonjour à tous,
voila j'ai un programme avec python 2.7 et j'ai utilisé quelques Threads mais quand je lance mon programme et je veux l’arrêter avec le bon vieux Ctrl+c impossible d’arrêter le programme
le problème vient des threads je pense mais comment resoudre le probleme ?
merci d'avance pour votre aide
Messages : 160
Sujets : 6
Inscription : May 2015
Bonjour,
Faut voir comment sont lancés les Threads car je pense qu'il faut les lancer en mode
deamon comme dans l'exemple ci-dessous:
Code :
#!/usr/bin/python
import time
import threading
class FirstThread (threading.Thread):
def run (self):
nf = 0
while True:
print '#{}: First thread in execution'.format(nf)
nf += 1
time.sleep(3)
class SecondThread (threading.Thread):
def run (self):
ns = 0
while True:
print '#{}: Second thread in execution'.format(ns)
ns += 1
time.sleep(1)
f = FirstThread()
f.daemon = True
f.start()
s = SecondThread()
s.daemon = True
s.start()
while True:
time.sleep(1)
Un CTRL-C arrête bien le programme
Code :
$ ./PythonCtrlCDaemons.py
#0: First thread in execution
#0: Second thread in execution
#1: Second thread in execution
#2: Second thread in execution
#1: First thread in execution
#3: Second thread in execution
#4: Second thread in execution
^CTraceback (most recent call last):
File "./PythonCtrlCDaemons.py", line 30, in <module>
time.sleep(1)
KeyboardInterrupt
$
Source:
Cannot kill Python script with Ctrl-C