Pregunta

I have some trouble using threads in python. For some reasons I have to create 2 threads. Each thread is doing almost the same thing : a loop that receive data and send them. My problem is : I don't know how to stop those threads. The data are sent continuously. I tried to use "KeyboardInterrupt" and "myThread.daemon", but the loop is still running in console. So if you know a solution to stop them (with 'CTRL+C' or a 'Keypressed'), I would be gratfefull.

Here is the global functioning of my code :

#=========#
#Thread 1 #
#=========#
class ThreadEmissionSerie1(threading.Thread):
        def __init__(self):
                threading.Thread.__init__(self)
        def run(self):

                variable = 0
                while 1:
                        try:
                                print "variable 1 : ", variable
                                variable += 2
                        except:
                                pass

#=========#
#Thread 2 #
#=========#
class ThreadEmissionSerie2(threading.Thread):
        def __init__(self):
                threading.Thread.__init__(self)
        def run(self):
                variable = 0
                while 1:
                        try:
                                print "variable 2 : ", variable
                                variable += 3
                        except:
                                pass


print "starting the threads"

th_E1 = ThreadEmissionSerie1()
th_E2 = ThreadEmissionSerie2()

th_E1.daemon = True
th_E2.daemon = True
th_E1.start()
th_E2.start()

I hope someone can help me :) This programme should run on Windows AND Linux Thanks, Arthur

¿Fue útil?

Solución

I think that the answer is as I commented: under IDLE, the "module" is running with IDLE's shell as the main thread, so the daemon threads do not die.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top