]> err.no Git - dak/commitdiff
dakmultiprocessing: close all session
authorAnsgar Burchardt <ansgar@debian.org>
Sat, 26 Mar 2011 17:09:11 +0000 (17:09 +0000)
committerAnsgar Burchardt <ansgar@debian.org>
Sat, 26 Mar 2011 17:09:11 +0000 (17:09 +0000)
Make sure to close all sessions so we do not run out of connections.
Before this exceptions in the workers might leave open transactions
around.

Signed-off-by: Ansgar Burchardt <ansgar@debian.org>
daklib/dakmultiprocessing.py

index 646f3633f0e4ffeca3fa270f3b4dafe114b68075..ded81a2902cf975161cb2c20d372a7f9bb1a477c 100644 (file)
@@ -26,6 +26,14 @@ multiprocessing for DAK
 ###############################################################################
 
 import multiprocessing
+import sqlalchemy.orm.session
+
+def _func_wrapper(func, *args, **kwds):
+    try:
+        return func(*args, **kwds)
+    finally:
+        # Make sure connections are closed. We might die otherwise.
+        sqlalchemy.orm.session.Session.close_all()
 
 class Pool():
     def __init__(self, *args, **kwds):
@@ -33,7 +41,9 @@ class Pool():
         self.results = []
 
     def apply_async(self, func, args=(), kwds={}, callback=None):
-        self.results.append(self.pool.apply_async(func, args, kwds, callback))
+        wrapper_args = list(args)
+        wrapper_args.insert(0, func)
+        self.results.append(self.pool.apply_async(_func_wrapper, wrapper_args, kwds, callback))
 
     def close(self):
         self.pool.close()