Using Django DB connection in custom threaded scripts
<p>There are many questions about is django db connection thread safe, but they all seem to be asking the default request threads.</p>

<p>What if I am writing custom script that uses database connection in threads:</p>

<pre><code>from django.db import connections
import threading

class Transform(object):
    def transform_data(self, listing):
        cursor = self.connection.cursor()
        cursor.execute('SELECT ... WHERE id = %s', listing.id)
        data  = cursor.fetchall()
        ...

    def run(self):
        connection = self.connections['legacy']
        for listing in listings:
            threading.Thread(target=self.transform_data, args=[listing])
</code></pre>

<p>How safe is data inside <code>transform_data</code> thread in terms of the result from cursor is not mixed up with other threads?</p>