Skip to content Skip to sidebar Skip to footer

Django Persistent Api Connections Between Requests

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 cr

Solution 1:

Quite simple really

conn = api.connect() # This line is run only once when the process starts and the module is loaded defview(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>>>defadd(b):...print a + b...>>>add(2)
3

Note that you can't modify the connection without using the global keyword

>>>defchange(c):...    a = c...print a...>>>change(4)
4
>>>print a
1

Compare:

>>>a = 1>>>defchange(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.

Solution 2:

In addition to aychedee's answer I would suggest you to take a look at django's database persistent connections, which are new for Django 1.6. The idea:

Persistent connections avoid the overhead of re-establishing a connection to the database in each request.

You can implement something similar for your own needs.

Code on Github in Django's 1.6 branch. It's complicated enough but contains connection expiring and usability check logic.

Post a Comment for "Django Persistent Api Connections Between Requests"