Python Forum
running two functions by threading
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
running two functions by threading
#1
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()
Reply
#2
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..
swallow osama bin laden
Reply
#3
i found the problem by adding infinity while true loop in function 2
that's because threading makes the function like another main program
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,718 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,076 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,468 Aug-25-2020, 03:43 AM
Last Post: micseydel
  Multiproccessing ValueError: Pool not running when running parallel functions Gilush 1 3,269 Jun-06-2020, 12:49 AM
Last Post: Gilush
  code keeps running if i use from threading import timer? birddseedd 3 2,565 Jan-25-2019, 05:00 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020