Receive our weekly advanced programming tips for FREE
02.May
How to use threads in python
0
| 0 votes
Autor: bookkeeper
Chapter: Python
Tags:

Python brings also thread in the box. The simple example is:

import threading
from time import sleep

class Thread ( threading.Thread ):
    def run(self):
        while True:
            print "running thread..."
            sleep(1)#wait one second

def main():
    t = Thread()
    t.start()
    t.join()

if __name__ == '__main__':
    main()

Every second the message 'running thread...' will be print.

To make it more usefull let's crawl website list and get the titles. We will have two thread one which get a item from the queue and other will feed it.

import threading
from time import sleep
import signal
import sys
from Queue import Queue
import urllib
import re

title_re = re.compile("\", re.I | re.M)

class ThreadCrawler(threading.Thread):
    def __init__(self, queue):
        self.queue = queue
        threading.Thread.__init__(self)

    def run(self):
        while True:
            url = self.queue.get()
            self.queue.task_done()

            try:
                data = urllib.urlopen(url).read()
                found = title_re.findall(data)

                print "url: ", url, " title: ",

                if found:
                    print  found[0]
                else:
                    print ""
            except Exception, e:#little bit ugly
                print e
           
class ThreadFeeder(threading.Thread):
    def __init__(self, queue):
        self.queue = queue
        threading.Thread.__init__(self)

    def run(self):
        while True:
            # put yu logic to to feed it
            link = self.queue.put('http://www.google.com')

q = Queue()

def main():
    c = ThreadCrawler(q)   
    f = ThreadFeeder(q)

    c.start()
    f.start()

    while True:
        pass

if __name__ == '__main__':
    main()

Class ThreadCrawler takes one item from the queue tells the queue with 'task_done' that it should unlock the queue. After it's fetches the data and parse it for the title. You schould customize class ThreadFeeder to your needs :)

Thread it just one part that python provides!