How to terminate a function in the middle when threading Python

Asked 2 years ago, Updated 2 years ago, 16 views

import time
from threading import Thread

def func1(off):
    while not off:
        for i in range(0,100):
            print (i)
            time.sleep(1)

def func2():
    total=0
    for i in range(0,30) :
        total += i
        time.sleep(0.5)

        if total >= 55 :
            return 1

off=0
t1=Thread(target=func1, args=(off,))
t2=Thread(target=func2)
t1.daemon=True
t2.daemon=True



a=t2.start()
off=a
t1.start()

input()

That's what I wrote What I want to do is

I want to end the func1 function by changing the off variable to 1 in the middle while executing the func1 function

The condition for terminating is that you thread two functions together like a func2 function

Which func1 function ends due to another function that is threading?

That's what I'm drawing The func1 function doesn't end well in the middle

It doesn't have to be that way We need a way to thread two functions and the other ends one

I'd appreciate it if you could help me

python

2022-09-20 20:48

1 Answers

import threading
import time

off = 0
class Thread1(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.flag = threading.Event()

    def run(self):
        while not self.flag.is_set():
            print("Thread1 said : Thread %s alive\n"%self.ident)
            time.sleep(1)
        print("Thread1 said : Thread %s die\n"%self.ident)

class Thread2(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.flag = threading.Event()

    def run(self):
        global off
        for i in range(0,5):
            print("Thread2 said : %d left\n"%(5-i))
            time.sleep(1)
        off = 1 
        print("Thread2 said : Thread %s die\n"%self.ident)

def main():
    try:
        j1 = Thread1()
        j2 = Thread2()
        j1.start()
        j2.start()

        j2.join()
        if off == 1:
            j1.flag.set()
            j1.join()
    except Exception as e:
        print(e)

if __name__ == '__main__':
    main()

#REF. https://www.g-loaded.eu/2016/11/24/how-to-terminate-running-python-threads-using-signals/
#REF2. https://docs.python.org/2/library/threading.html#event-objects

Please refer to this.

Additional. It's based on the code you posted.

import time
import threading

def func1():
    global flag
    flag = threading.Event()
    while not flag.is_set():
        for i in range(0,50):
            time.sleep(0.2)
    print("func1 die")

def func2():
    total=0
    for i in range(0,30) :
        total += i
        time.sleep(0.5)
        if total >= 55 :
            print("func2 die")
            return 1

t1=threading.Thread(target=func1)
t2=threading.Thread(target=func2)
t1.daemon=True
t2.daemon=True

t1.start()
t2.start()
t2.join()
flag.set()
t1.join()

print("exit") #input()


2022-09-20 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.