Skip to content

Instantly share code, notes, and snippets.

@smit1678
Created January 23, 2014 14:00
Show Gist options
  • Save smit1678/8578833 to your computer and use it in GitHub Desktop.
Save smit1678/8578833 to your computer and use it in GitHub Desktop.
class ThreadXML(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#grabs file from queue
fn = self.queue.get()
#grabs file and processes
xmlProcess(fn)
#signals to queue job is done
self.queue.task_done()
def main():
#spawn a pool of threads, and pass them queue instance
for i in range(5):
t = ThreadXML(queue)
t.setDaemon(True)
t.start()
#populate queue with data
for fn in os.listdir('.'):
if fn.endswith(".xml"):
queue.put(fn)
#wait on the queue until everything has been processed
queue.join()
main()
# credit: http://www.ibm.com/developerworks/aix/library/au-threadingpython/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment