“Python mata a todos los hilos” Código de respuesta

Python mata a todos los hilos

#setting thread as a daemon thread

import threading
import time
import sys
def exfu():
    while True:
        time.sleep(0.5)
        print('Thread alive, but it will die on program termination')
x = threading.Thread(target=exfu)
x.daemon = True
x.start()
time.sleep(2)
sys.exit()
ykk

Python mata a todos los hilos

#using _stop()

import time
import threading
 
class th1(threading.Thread):
    def __init__(self, *args, **kwargs):
        super(th1, self).__init__(*args, **kwargs)
        self._stop = threading.Event()
    def stop(self):
        self._stop.set()
    def stopped(self):
        return self._stop.isSet()
    def run(self):
        while True:
            if self.stopped():
                return
            print("Hello, world!")
            time.sleep(1)
 
x = th1()
x.start()
time.sleep(5)
x.stop()
x.join()
ykk

Respuestas similares a “Python mata a todos los hilos”

Preguntas similares a “Python mata a todos los hilos”

Más respuestas relacionadas con “Python mata a todos los hilos” en Python

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código