[Python] Why the cycle running in main proccess wont stop after being class state modified...

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Consider this sample:

    import multiprocessing as mp
    import time

    class CycleThatDoesntStop:
    shouldrun = True

    def run(self):
    while self.shouldrun:
    print("im alive", self.shouldrun)
    time.sleep(0.5)

    def stop(self):
    print("stop was called!")
    self.shouldrun = False

    def stopper(cycle_inst):
    time.sleep(0.6)
    print("stopping...")
    cycle_inst.stop()
    print("done?")

    if __name__ == '__main__':
    cycler = CycleThatDoesntStop()
    p = mp.Process(target=stopper, args=(cycler,))
    p.start()
    cycler.run()
    p.join()


    Why does the cycle continues to go on? Is it because stopper func doesn't get access to memory, though there is no exceptions?

    Continue reading...

Compartilhe esta Página