Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto connect_xxx method and connection pools

If I call boto.connect_xxx, where xxx is some service (dynamodb, s3, etc) multiple times, does it create a new connection pool each time? What I'd like to do is something like this (example in Flask):

@app.before_request
def before_request():
    g.db = connect_dynamodb()

to make sure I always connect, but I don't want to do this before each request if it will create new security tokens, etc, the whole rigamarole, each time. Is it safe to just call connect_xxx() once when the application starts, and rely on boto to generate new connections as needed, etc.?

like image 947
Caleb Wright Avatar asked Jul 24 '12 00:07

Caleb Wright


1 Answers

The best approach is to call the connect_xxx method once when your application starts and rely on boto to manage the connection from then on. The only exception to that rule is if you are using multiple threads. In that case, each thread should create it's own connection since boto uses httplib which is not threadsafe.

Even if you call the connect_xxx method before each request, you really should be okay. Boto pools connection at the class level and should handle this in a pretty efficient manner.

like image 64
garnaat Avatar answered Sep 19 '22 16:09

garnaat