Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 8th, 2004, 02:18 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
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

Reply With Quote
  #2  
Old July 8th, 2004, 04:13 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 12
Send a message via MSN to Grim Archon
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
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old July 8th, 2004, 12:45 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > while loop & threading

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap