0

I have a web app that has been built using the Pyramid framework. I would like to add functionality to connect to a SOAP API. For this I have successfully been able to use the suds library.

When using suds, I first create a client as follows:

from suds.client import Client
client = Client(wsdl_url)

Now for each user of my web app I will maintain a different authentication token which I will pass along with each call I make to the SOAP API. What I would like to avoid doing is re-building the suds client every single time I need to hit the API.

Sometimes the front end of my web app will make a dozen nearly simultaneous ajax requests to my server, which I will need to pass along to the API.

I would like to build the client once, when the server is re-booted, instead of rebuilding it each time I need to make an API call. Continually re-parsing the XML just to keep building the same suds client object over and over seems like a waste to me.

tadasajon
  • 14,276
  • 29
  • 92
  • 144

1 Answers1

1

Well the issue of anything global is whether it is threadsafe or not. If it is, you can simply store it on the registry and allow everyone to access it:

config.registry['wsdl_client'] = Client(wsdl_url)

def some_view(request):
    client = request.registry['wsdl_client']

However, if this is something that's not immediately shareable between threads you will have to either create a new object per request, as you're complaining about, or store a new instance per thread using a threadlocal. The thing to be careful of is properly handling setup/teardown of the object when you're sharing it between requests. For example, when threadlocal objects are usually used in Pyramid, they need to be cleaned up at the end of a request via request.add_finished_callback(). Sorry I can't comment specifically on suds, but these are your basic options.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70