Python - Multithread to read one file

Today, i am working with python. I need write script to read one file, and get line by line, per line deliver one thread process ( total 10 threads). I want solution, so i chose working with thread and queue.
In python, when procsess initializate, this process will be assigned with queue, and working with this queue. We will put data ( in this case is line) to queue. Process will read from queue, so, all processes can read one file, not overlap :D

import threading
import Queue

#Number of threads
n_thread = 5
#Create queue
queue = Queue.Queue()

class ThreadClass(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
    #Assign thread working with queue
        self.queue = queue

    def run(self):
        while True:
        #Get from queue job
            host = self.queue.get()
            print self.getName() + ":" + host
        #signals to queue job is done
            self.queue.task_done()

#Create number process
for i in range(n_thread):
    t = ThreadClass(queue)
    t.setDaemon(True)
    #Start thread
    t.start()

#Read file line by line
hostfile = open("hosts.txt","r")
for line in hostfile:
    #Put line to queue
    queue.put(line)
#wait on the queue until everything has been processed
queue.join()

------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.  

Comments

Anonymous said…
thanks!!!!
Unknown said…
hello kendy, thank you for saving my ass. I have a file consisting of 18k ip address, need to read line by line and telnet and configure.
Anonymous said…
Beautiful! Thanks!
Shahid Mahmood said…
This is beautiful!! Thankyou Kendy...
Hi there, I found your website by means of Google while looking for a
similar matter, your site got here up, it looks great. I have bookmarked it in my google bookmarks. BLUE COOKIES MEDICAL KUSH FOR ALL I have got you bookmarked to look at new stuff you .

Popular posts from this blog

OpenCA tutorial

ATTT Ứng dụng Web - PHẦN 3: BẢO VỆ AN TOÀN THÔNG TIN ỨNG DỤNG WEB