2

I am writing a script that requires interacting with several databases (not concurrently). In order to facilitate this, I am mainting the db related information (connections etc) in a dictionary. As an aside, I am using sqlAlchemy for all interaction with the db. I don't know whether that is relevant to this question or not.

I have a function to set up the pool. It looks somewhat like this:

def setupPool():
    global pooled_objects

    for name in NAMES:
        engine = create_engine("postgresql+psycopg2://postgres:pwd@localhost/%s" % name)
        metadata = MetaData(engine)
        conn = engine.connect()
        tbl = Table('my_table', metadata, autoload=True)
        info = {'db_connection': conn, 'table': tbl }

        pooled_objects[name] = info

I am not sure if there are any gotchas in the code above, since I am using the same variable names, and its not clear (to me atleast), how the underlying pointers to the resources (connection are being handled). For example, will creating another engine (to a different db) and assigning it to the 'engine' variable cause the previous instance to be 'harvested' by the GC (since no code is using that reference yet - the pool is still being setup).

In short, is the code above OK?, and if not, why not - i.e. how may I fix it with respect to the issues mentioned above?

1 Answer 1

4

The code you have is perfectly good.

Just because you use the same variable name does not mean you are overriding (or freeing) another object that was assigned to that variable. In fact, you can look at the names as temporary labels to your objects.

Now, you store the final objects in the global dictionary pooled_objects, which means that until your program is done or your delete data from there explicitely, GC is not going to free them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.