|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
Threading
Does anybody have any small multithreading examples using the threading module?
|
|
#2
|
||||
|
||||
|
Read this :
http://heather.cs.ucdavis.edu/~matl...n/PyThreads.pdf This should get you relatively familiar with the basics of threading.
__________________
Time is the greatest of teachers ; sadly, it kills all of its students. - Hector Berlioz |
|
#3
|
|||
|
|||
|
Thanks, nice link
![]() |
|
#4
|
||||
|
||||
|
Quote:
Using the threading module that wraps the thread module: http://forums.devshed.com/t120204/s...light=threading Grim |
|
#5
|
|||
|
|||
|
Quote:
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. ![]() |
|
#6
|
||||
|
||||
|
Quote:
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 ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#7
|
|||
|
|||
|
what's threading??
|
|
#8
|
||||
|
||||
|
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. |
|
#9
|
|||
|
|||
|
Quote:
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. |
|
#10
|
|||
|
|||
|
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) |
|
#11
|
||||
|
||||
|
Quote:
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). |
|
#12
|
|||
|
|||
|
Thanks
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Threading |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|