python sqlalchemy + postgresql program freezes -
i've ran strange situation. i'm writing test cases program. program written work on sqllite or postgresqul depending on preferences. i'm writing test code using unittest. i'm doing:
def setup(self): """ reset database before each test. """ if os.path.exists(root_storage): shutil.rmtree(root_storage) reset_database() initialize_startup() self.project_service = projectservice() self.structure_helper = fileshelper() user = model.user("test_user", "test_pass", "test_mail@tvb.org", true, "user") self.test_user = dao.store_entity(user)
in setup remove folders exist(created tests) reset database (drop tables cascade basically) initialize database again , create services used testing.
def teardown(self): """ remove project folders , clean database. """ created_projects = dao.get_projects_for_user(self.test_user.id) project in created_projects: self.structure_helper.remove_project_structure(project.name) reset_database()
tear down same thing except creating services, because test module part of same suite other modules , don't want things left behind tests.
now tests run fine sqllite. postgresql i'm running weird situation: @ point in execution, differs run run small margin (ex 1 or 2 calls) program halts. mean no error generated, no exception thrown, program stops.
now thing can think of somehow forget connection opened somewhere , after while timesout , happens. have lot of connections before start going trough code, appreciate suggestions/ opinions.
what cause kind of behaviour? start looking?
regards, bogdan
postgresql based applications freeze because pg locks tables aggressively, in particular not allow drop command continue if connections open in pending transaction, have accessed table in way (select included).
if you're on unix system, command "ps -ef | grep 'post'" show postgresql processes , you'll see status of current commands, including hung "drop table" or whatever that's freezing. can see if select pg_stat_activity view.
so key ensure no pending transactions remain - means @ dbapi level result cursors closed, , connection open has rollback()
called on it, or otherwise explicitly closed. in sqlalchemy, means result sets (i.e. resultproxy
) pending rows exhausted , connection
objects have been close()
d, returns them pool , calls rollback()
on underlying dbapi connection. you'd want make sure there kind of unconditional teardown code makes sure happens before drop table type of command emitted.
as far "i have lot of connections", should under control. when sqla test suite runs through 3000 tests, make sure we're absolutely in control of connections , typically 1 connection opened @ time (still, running on pypy has behaviors still cause hangs pg..its tough). there's pool class called assertionpool
can use ensures 1 connection ever checked out @ time else informative error raised (shows checked out).
Comments
Post a Comment