[Python] python notify thread exit with multiple loops

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I have code with two loops in thread, I wish to exit the thread after done in main thread.

    Currently I use a global variable to notify it but it looks not good since multiple thread access the same variable. And code looks ugly to break in multiple loops.

    What's graceful way to exit thread in this case?

    #!/usr/bin/env python3
    import subprocess
    import threading

    bexit = False
    def threadLoop(idx):
    global bexit
    while bexit == False:
    #do something here
    while True:
    #do something here
    if bexit == True:
    break
    if bexit == True:
    break
    bexit = False
    return

    def main():
    global bexit
    thread = threading.Thread(target=threadLoop, args=(0, ))
    thread.start()
    #do something here
    bexit = True
    thread.join()
    return

    main(

    Continue reading...

Compartilhe esta Página