/thread_close_connections.py Secret
Created
May 11, 2018 04:22
Star
You must be signed in to star a gist
Close database connections in thread in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread | |
from django.db import connections | |
class APIThread(Thread) | |
def __init__(self, data): | |
self.data = data | |
Thread.__init__(self) | |
def run(self) | |
# call external api | |
# process return value | |
# Django model database access | |
# close all connections in the thread | |
for conn in connections: | |
conn.close() | |
# create new thread to call api | |
def make_thread(data) | |
APIThread(data).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment