Re: [Fedora-suds-list] Integration into a async framework
by Renato Alves
import threading
import Queue
from twisted.web import xmlrpc, server
from twisted.internet import reactor
(...)
class ThreadedSuds(threading.Thread):
def run(self):
# Prepare SOAP client
# SServer is a class wrapper with some stuff specific to the
# soap server I'm using, but basically sets up a suds client
# instance
soap = SServer(url='file:./wsdl',
user='xxxx',
passwd='xxxx')
# Make thread run until STOPped
while True:
try:
data = seq_queue.get(True, 60)
except Queue.Empty:
# Checks for ticket validity and requests a new one if
# expired
soap.check_ticket()
data = None
if data is not None:
ticket, seq = data
# Do your soap requests here
(...)
# Declare ticket as finished and return results
soap_results[ticket] = results
(...)
class SequenceServer(xmlrpc.XMLRPC):
"""Broker calls
"""
def xmlrpc_ping(self):
"""Reply with pong to let know server is alive
"""
return 'pong'
def xmlrpc_includeseq(self, seq):
"""Put seq in queue
"""
# generate a new ticket (MD5)
ticket = generate_ticket(seq)
# queue the sequence
seq_queue.put([ticket, seq])
return ticket
(...)
def main():
# queue should be accessible to all threads
global sequence_queue
# results should also be accessible to all threads
global soap_results
sequence_queue = Queue.Queue(100)
soap_results = dict()
# Start 5 threads for SOAP client
threads = [ThreadedSuds() for x in range(5)]
for thread in threads:
# Daemonize threads - if (thread = daemon) program will close
# without waiting for threads to finish
# New API > 2.6.*
thread.daemon = True
# Old API < 2.5.*
thread.setDaemon(True)
# Start threads
thread.start()
f = server.Site(SequenceServer())
reactor.listenTCP(7080, f, interface="localhost")
reactor.run()
if __name__ == '__main__':
main()
--
Renato Alves
15 years, 3 months