Thread: Threading

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Threading


    Does anybody have any small multithreading examples using the threading module?
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    Read this :

    http://heather.cs.ucdavis.edu/~matlo.../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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks, nice link
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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....ight=threading
    Grim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Originally Posted by Grim Archon
    Using the threading module that wraps the thread module:
    http://forums.devshed.com/t120204/s....ight=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.
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    44
    Rep Power
    11
    what's threading??
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    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 10:03 AM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep 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)
  20. #11
  21. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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).
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo