Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env
- from Queue import *
- from threading import Thread, Lock
- # this function will process the
- # items in the queue, in serial
- def processor():
- if queue.empty() == True:
- # comparison to True should
- # be 'if cond is True:' or
- # 'if cond:' [pep8]
- print "Queue Empty"
- sys.exit(1)
- try:
- job = queue.get()
- print "I'm operating on job item: %s" % (job)
- queue.task_done()
- except:
- print "Failed to work on job"
- # set variables
- queue = Queue()
- threads = 4
- # your list of job items
- jobs = ["job1", "job2", "job3"]
- # iterate over jobs and put each into
- # the queue in sequence
- for job in jobs:
- print "Adding job to queue: %s" % (job)
- queue.put(job)
- # start threads, each one will process
- # one job from the queue
- for i in range(threads):
- th = Thread(target=processor)
- th.setDaemon(True)
- th.start()
- # wait untill all jobs are processed before quitting
- queue.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement