Every so often you need to use a queue to manage operations in an
application. Python makes this very simple. Python also, as I’ve
written about before, makes threading very easy to work with. So
in this quick program I’ll describe via comments, how to make a
simple queue where each job is processed by a thread. Integrating
this code to read jobs from a mysql database would be trivial as
well; simply replace the “jobs = [..." code with a database call
to a row select query.
#!/usr/bin/env python
## DATE: 2011-01-20
## FILE: queue.py
## AUTHOR: Matt Reid
## WEBSITE: http://themattreid.com
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:
print "the Queue is empty!"
sys.exit(1)
try:
job = queue.get()
print "I'm operating on job item: %s"%(job)
queue.task_done()
except:
print …
[Read more]