473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Job queue using xmlrpc and threading

I'm trying to implement an application that will listen for requests,
run them when they are present but also be able to add new requests
even while it's running. I've tried to do this using the thread and
xmlrpc modules - the idea is that an XML-RPC exposed object tell the
queue thread object to add a job. If there are no jobs running, it
creates a file, adds the new job to the end and then another
consumption thread starts working through the jobs in the file. New
jobs coming in are just added to the end of the file by the queue
thread.

Unfortunately, I can't get it to work. The problem is that the
consumption thread seems to read the job queue before it gets written,
even though I've used a lock. I've also had the application get to the
stage where it ignores ctrl-c, which is a little worrying - I fear it
doesn't bode well for future stability... I don't have a lot of
experience with multi-threaded applications, so I may well have chosen
a poor approach.

I've posted the code below. It's in three parts, the job queue, the
manager that listens for new requests and an application to add jobs
to the queue. Sorry for the long listings...

Any guidance gratefully received,

Peter

===
testqueue.py:

import thread
import time
import shutil
import os

class JobQueue:

def __init__(self, filename):
self.queuefile = filename
self.jobthread = 0
# lock for the jobfile queue file
self.jfqlock = thread.allocate_lock()

def addJob(self, jobfileuri, email):
self.jfqlock.acquire()
if not self.jobthread:
print "starting jobfile consumption thread"
if os.access(self.queuefile, os.R_OK):
print "cleaning stale jobfile queue file"
try:
os.remove(self.queuefile)
except:
print "problem removing jobfile queue file"
raise
self.jobthread = thread.start_new_thread(self.main, ())
else:
print "using existing jobfile consumption thread in file",
self.queuefile
fh = open(self.queuefile, "a")
# choose "::::" as a delimiter
print >fh, jobfileuri + "::::" + email
self.jfqlock.release()
return 1

def main(self):
while 1:
self.jfqlock.acquire()
rfh = open(self.queuefile, "r")
# breakpoint()
finput = rfh.readline()
print "found:", finput
if not finput:
print "finished jobfiles. Closing thread"
rfh.close()
self.jobthread = 0
self.jfqlock.release()
return
else:
print "found jobfile in queue: attempting to run"
# most of this is to shift up the lines in the file
tmpname = self.queuefile + ".tmp"
wfh = open(tmpname, "w")
line = rfh.readline()
while line:
wfh.write(line)
line = rfh.readline()
wfh.close()
rfh.close()
shutil.move(tmpname, self.queuefile)
self.jfqlock.release()
# lop off the trailing line break
print
print "***run Starting***"
try:
self.runJob(finput[:-1])
print "***run finished***"
except:
print "***run failed***"
print

def runJob(self, job):
time.sleep(2.0)
print "running job", job
if not report:
print "some problem with run. Cannot mail out report"
return
===
queuemanager.py

from testqueue import JobQueue
from SimpleXMLRPCServer import *
class QM:
def __init__(self, filename):
self.jq = JobQueue("queue.txt")

def addJob(self, jobname):
self.jq.addJob(jobname, "tester@testdomain")

if __name__=="__main__":
qm = QM("jobqueue.txt")
rpcserver = SimpleXMLRPCServer(("localhost", 8000))
rpcserver.register_instance(qm)
rpcserver.serve_forever()

===
addjob.py:

import xmlrpclib
import sys

server = "localhost"
port = 8000

serveradd = "http://%s:%s" % (server, port)
manager = xmlrpclib.ServerProxy(serveradd)

jobname = sys.argv[1]

manager.addJob(jobname)
Sep 22 '08 #1
1 2526
Try using the Queue module - http://docs.python.org/lib/module-Queue.html.
Here is a tutorial with it - http://www.artfulcode.net/articles/m...eading-python/.
Sep 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
9
by: mateom | last post by:
Hello, I'm using Queue to send images from one thread to another, and some of the images are not appearing in the consumer thread....maybe 1 in 3 arrive. I've tried passing the image data in...
13
by: Jonathan Amsterdam | last post by:
I think there's a slight design flaw in the Queue class that makes it hard to avoid nested monitor deadlock. The problem is that the mutex used by the Queue is not easy to change. You can then...
2
by: shipcreak | last post by:
I have an interesting problem with a sort of producer-consumer system for error logging. Consider the following code: <code> SyncLock _eventList.SyncRoot Dim item As ExceptionLogEntry '...
0
by: Michael Bayer | last post by:
Hi - i was just going through this thread: http://mail.python.org/ pipermail/python-list/2006-April/336948.html , where it is suggested that the Lock instance used by Queue.Queue should be...
2
by: Thomas Ploch | last post by:
Hello folks, I am having troubles with implementing a timed queue. I am using the 'Queue' module to manage several queues. But I want a timed access, i.e. only 2 fetches per second max. I am...
3
by: seb | last post by:
Hi, I am writing to a file some basic information using the logging module. It is working but in the log file some line are printed several time. I had put some print debugging messages in the...
0
by: Eugene Anthony | last post by:
First I leave the server running. Then I open up the client and click the button to send a message to the queue. So when I check the queue its empty. Possible because all the message in the queue...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.