#1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    while loop & threading


    Ok i have been reading a few doc in threading.. still in the newbie stage of it though but.. how do i turn this in to a while loop instead of a for loop but inside the run( ) function? i have tried and tried.. and all i get is my program to run and run and run and run even if i pressed ctrl+v it would still run ( i have created an evil program :P ) here is the one with the for loop

    Code:
    import threading, time
                                                                                                                 
                                                                                                                 
    class Threads( threading.Thread ):
                                                                                                                 
            def __init__( self, data ):
                                                                                                                 
                    self.data   = data
                                                                                                                 
                    threading.Thread.__init__( self )
                    self.setDaemon( 1 )
                                                                                                                 
            def run( self ):
                                                                                                                 
                    print "Text -> %s" % ( self.data )
                                                                                                                 
                                                                                                                 
    listd = ["sup jo", "Do i know you?"]
                                                                                                                 
    for x in listd:
                                                                                                                 
            test = Threads( x )
            test.start( )
            time.sleep( 1.0 )
            test.join( )
                                                                                                                 
    print 'Done!!'
    it works.. well but.. like i said.. im still new to threading and need it in a while loop for future gui programming .. and im still having a hard time tring to find a well built documentation in threading. but so far i understand the above .. also any suggestion to make it better would be great
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    At the moment your program does not require threads at all. In the for loop it starts a thread then waits for that thread to finish (the join) before starting the next one.

    IMO what your require is the use of events.
    The principle is that each thread is started (you can use the for loop but drop the join )

    Then the main program busy waits in a while loop monitoring the events- at this point the main thread and child threads are all running in that time sliced multi-tasking mode. The main thread will wait for each threads event before deciding it has finished.
    Code:
    import threading, time
                                                                                                                 
                                                                                                                 
    class Threads(threading.Thread): 
                                                                                                                 
            def __init__(self, data, signal): 
                                                                                                                 
                    self.data = data
                    self.signal = signal
                                                                                                                 
                    threading.Thread.__init__(self)
                    self.setDaemon(1)
                                                                                                                 
            def run(self): 
                    time.sleep(1)
                    print "Text -> %s"%(self.data)
                    self.signal.set()
                                                                                                                 
                                                                                                                 
    listd = ["sup jo", "Do i know you?"]
                                                                                                                 
    slaves = []
    for x in listd: 
        sig = threading.Event()
        sig.clear() #make sure the event is not set yet
        test = Threads(x, sig)
        test.start()
        slaves.append((test, sig)) # Keep a copy of the thread and event
        
    print "Waiting"
    while len(slaves): 
        print ".", 
        for n in slaves:  # check each slave
            if not n[1].isSet():  # test to see if the thread has triggered it's event
                continue # at least one thread is still running so we continue monitoring
            n[0].join() #just to be sure we have tidied up
            slaves.pop(0) # Because I use continue  we only get here on index 0
            print "A slave is dead"
            
    print 'Done!!'
    By the way do you really need the setDaemon in this situation?

    grim
  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    i setDaemon( ) cause i found a pdf file in threading and it hade examples in there so i was tring things out all sort of different ways.. cause i really want to learn threading.. just the my time is limited with school work and my son .. but anyways i will attach the pdf file soon so that maybe others can take a look at it who is at the same level as i am.. for now i copied your code so that i can study it once i get back from school , also i have seen a tutorial in threading that was using the threading event and time.sleep( ) so that when time.sleep( ) reached its value ( ie time.sleep(10)) it stops.. but cant find it anyware anymore :S wanted to learn it that way as well.. anyways thanx again grim
    Last edited by xlordt; July 8th, 2004 at 12:53 PM.

IMN logo majestic logo threadwatch logo seochat tools logo