Close connections in thread

pull/40/head
Piero Toffanin 2016-10-29 20:28:37 -04:00
rodzic 100fec6808
commit af323a1549
2 zmienionych plików z 17 dodań i 2 usunięć

Wyświetl plik

@ -91,7 +91,7 @@ class TaskViewSet(viewsets.ViewSet):
serializer.save()
# Call the scheduler (speed things up)
#scheduler.process_pending_tasks(background=True)
scheduler.process_pending_tasks(background=True)
return Response(serializer.data)

Wyświetl plik

@ -6,6 +6,7 @@ from multiprocessing.dummy import Pool as ThreadPool
from nodeodm.models import ProcessingNode
from app.models import Task
from django.db.models import Q
from django import db
import random
logger = logging.getLogger('app.logger')
@ -18,8 +19,22 @@ def background(func):
"""
def wrapper(*args,**kwargs):
background = kwargs.get('background', False)
if 'background' in kwargs: del kwargs['background']
if background:
t = Thread(target=func)
# Create a function that closes all
# db connections at the end of the thread
# This is necessary to make sure we don't leave
# open connections lying around.
def execute_and_close_db():
ret = None
try:
ret = func(*args, **kwargs)
finally:
db.connections.close_all()
return ret
t = Thread(target=execute_and_close_db)
t.start()
return t
else: