The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Threading
Discuss Threading in the Python Programming forum on Dev Shed. Threading Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 17th, 2004, 04:53 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Threading
Does anybody have any small multithreading examples using the threading module?
|

February 17th, 2004, 05:06 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
|
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz
|

February 17th, 2004, 06:22 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks, nice link 
|

February 18th, 2004, 04:27 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
|

February 23rd, 2004, 06:56 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Grim Archon |
For the most part I understand your example, but I'm not sure how to create a bunch of those threads (arbitrary amound). From what I can see, you only create one. 
|

February 24th, 2004, 04:30 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Quote: | Originally Posted by XxChris For the most part I understand your example, but I'm not sure how to create a bunch of those threads (arbitrary amound). From what I can see, you only create one.  |
No problem 
The pinger class based on the threading class can be instantiated as many times as you need. I have modified my pinger example to show this:
Code:
import threading
import popen2
import time
running = False
class pinger(threading.Thread):
def __init__(self,num,who,id=0):
self.num = num
self.who = who
self.id = id
threading.Thread.__init__(self)
def run(self):
global result
cmd = "ping -n %s %s"%(self.num,self.who)
fin,fout = popen2.popen4(cmd)
while running:
line = fin.readline()
if line.strip():
print "%s) %s"%(self.id,line)
if not line:
break
fin.close()
if __name__ == "__main__":
running = True
urls = ["127.0.0.1","www.bbc.co.uk","www.google.com"]
now = time.time()
end = now+300
ThreadsAtStart = threading.activeCount()
print "There are %s threads, we will now create %s pinger threads."%(ThreadsAtStart,len(urls))
for id,url in enumerate(urls):
ping = pinger(5,url,id)
ping.start()
while True:
print "Active threads %s"%threading.activeCount()
time.sleep(1)
if time.time() > end:
print "Timeout"
running = False
break
if threading.activeCount() == ThreadsAtStart:
print "Finished"
break
It also should demonstrate that you cannot rely on any particular order of execution (the printed output order will be different each time).
Cheers,
Grim 
|

February 24th, 2004, 04:59 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 44
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
what's threading??
|

February 24th, 2004, 06:48 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Whats the best way to explain this. I've always ended up thinking of threading is kinda like splitting hairs, or like fork lightening... when you start a new thread you're actually executing part of you're program in a new process, different from the other.
Hope you're with me here  , if you do a search of the forum you should get a few helpful insights.
Mark.
__________________
programming language development: www.netytan.com – Hula
|

February 24th, 2004, 09:00 AM
|
|
|
It's like having a three-lane motorway instead of a single-lane road.
Each car can still only do 70mph, but now the normal cars don't get stuck in a queue behind the 20mph truck - so you get more vehicles through in total, and the fast ones get there quickly regardless of the slow drivers and the breakdowns that would normally get in the way.
So instead of your code doing:
Code:
Time -->
Start - read from file - read from DB - read from network - End
You get:
Code:
Time -->
/ Read from file \
Start - Read from DB - End
\ Read from Net /
This is why some programs appear to "freeze" when accessing network files or cd-roms, or when doing heavy processing - they have to wait for the one thing to complete before they update the user interface. Threading allows them to update the user interface while reading from the slow device at the same time.
(NB: All this assumes you have free processor cycles - if something is using 100% of the CPU, making it threaded probably wont help. If it is using 10% of the CPU and waiting a lot for a slow network connection, it might well help.)
Last edited by sfb : February 24th, 2004 at 09:03 AM.
|

February 24th, 2004, 06:06 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Ahhh Thanks! One more question though: What exactly does this line do and is it necessary for all classes that subclass threading.Thread?
Code:
threading.Thread.__init__(self)
|

February 25th, 2004, 02:05 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Quote: | Originally Posted by XxChris Ahhh Thanks! One more question though: What exactly does this line do and is it necessary for all classes that subclass threading.Thread?
Code:
threading.Thread.__init__(self)
|
Quoting the threading documentation ....
"If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.
"
A quick look at the module Lib\threading.py shows it sets a lot of flags to initial conditions and makes sure the calling thread is not a daemon. (Daemon - a process running in the background not associated with a terminal session. e.g. Unix httpd, Windows - any service).
|

February 25th, 2004, 02:23 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|