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 February 17th, 2004, 04:53 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Threading

Does anybody have any small multithreading examples using the threading module?

Reply With Quote
  #2  
Old February 17th, 2004, 05:06 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
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

Reply With Quote
  #3  
Old February 17th, 2004, 06:22 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks, nice link

Reply With Quote
  #4  
Old February 18th, 2004, 04:27 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
Quote:
Originally Posted by XxChris
Does anybody have any small multithreading examples using the threading module?

Using the threading module that wraps the thread module:
http://forums.devshed.com/t120204/s...light=threading
Grim

Reply With Quote
  #5  
Old February 23rd, 2004, 06:56 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Grim Archon
Using the threading module that wraps the thread module:
http://forums.devshed.com/t120204/s...light=threading
Grim


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.

Reply With Quote
  #6  
Old February 24th, 2004, 04:30 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
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
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #7  
Old February 24th, 2004, 04:59 AM
flixxer flixxer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 44 flixxer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
what's threading??

Reply With Quote
  #8  
Old February 24th, 2004, 06:48 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #9  
Old February 24th, 2004, 09:00 AM
sfb sfb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Posts: 623 sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 5 h 53 m 21 sec
Reputation Power: 33
Quote:
what's threading??


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.

Reply With Quote
  #10  
Old February 24th, 2004, 06:06 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #11  
Old February 25th, 2004, 02:05 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
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).

Reply With Quote
  #12  
Old February 25th, 2004, 02:23 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > 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