Python Forum

Full Version: running two functions by threading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I'm trying to play two function at same time but one of this two function works only one time



import threading, time, sys





class counter :
    
    def num(self):
        for i in range (60):
            print(i)
            time.sleep(.5)

    def handler(self):
        print(' Processing .. ')
        time.sleep(3)





th1 = threading.Thread(target = counter().num).start()
th2 = threading.Thread(target = counter().handler).start()
its working correctly...

just track down th2 activities by adding print statement in line 17 (same indented level below time.sleep(3), print 'ok' for example ). th2 will print 'ok' exactly at 3 secs while th1 progresses..
i found the problem by adding infinity while true loop in function 2
that's because threading makes the function like another main program