Python Forum
running two functions by threading - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: running two functions by threading (/thread-8205.html)



running two functions by threading - Fathy - Feb-09-2018

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()



RE: running two functions by threading - ka06059 - Feb-10-2018

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..


RE: running two functions by threading - Fathy - Feb-10-2018

i found the problem by adding infinity while true loop in function 2
that's because threading makes the function like another main program