So I have a lot of internal and external APIs that are called on basically each request. This means that there's a lot of setting up connections to these APIs. Is there a way of creating a persistent connection object that can be shared between requests?
So I'd like to replace:
def a(request):
c = api.connect()
c.call_function()
With:
def b(request):
// use existing connection object from earlier request
c.call_function()
Any ideas?
I'm also not sure how big the gain would be but I don't mind doing some benchmarking once I have a first solution.
Quite simple really
conn = api.connect() # This line is run only once when the process starts and the module is loaded
def view(request):
conn.call_function() # This line is run every time a request is received
This connection would be shared by any request using the same worker/server process. So if you have three workers serving your application you would have at most three connections.
I would worry that the connections might start timing out. So you would want to guard against that. Perhaps by having a function that checked the state of the connection, returned it if it was still good, or creating a new one if it had expired.
Why this works can be illustrated with the following example:
>>> a = 1
>>> def add(b):
... print a + b
...
>>> add(2)
3
Note that you can't modify the connection without using the global keyword
>>> def change(c):
... a = c
... print a
...
>>> change(4)
4
>>> print a
1
Compare:
>>> a = 1
>>> def change(d):
... global a
... a = d
... print a
...
>>> change(5)
5
>>> print a
5
>>>
If you want to share the api connection between different workers/processes it becomes a bit trickier. i.e don't bother.