Can Django run in a threaded model? -
i looking through code base, particularly database connectivity parts, , came accross issue.
first, 1 gets cursor database using following stamentent:
from django.db import connection, transaction cursor = connection.cursor()
connection module attribute, in threaded model, threads share variable. sounds bit strange. diving in further, cursor() method belongs django.db.backends.basedatabasewrapper , looks this:
def cursor(self): self.validate_thread_sharing() if (self.use_debug_cursor or (self.use_debug_cursor none , settings.debug)): cursor = self.make_debug_cursor(self._cursor()) else: cursor = util.cursorwrapper(self._cursor(), self) return cursor
the key call _cursor(), executes backend code whatever database being used. in case of mysql, executes _cursor() method on django.db.backends.mysql.databasewrapper, looks like:
def _cursor(self): new_connection = false if not self._valid_connection(): new_connection = true kwargs = { 'conv': django_conversions, 'charset': 'utf8', 'use_unicode': true, } settings_dict = self.settings_dict if settings_dict['user']: kwargs['user'] = settings_dict['user'] if settings_dict['name']: kwargs['db'] = settings_dict['name'] if settings_dict['password']: kwargs['passwd'] = settings_dict['password'] if settings_dict['host'].startswith('/'): kwargs['unix_socket'] = settings_dict['host'] elif settings_dict['host']: kwargs['host'] = settings_dict['host'] if settings_dict['port']: kwargs['port'] = int(settings_dict['port']) # need number of potentially affected rows after # "update", not number of changed rows. kwargs['client_flag'] = client.found_rows kwargs.update(settings_dict['options']) self.connection = database.connect(**kwargs) self.connection.encoders[safeunicode] = self.connection.encoders[unicode] self.connection.encoders[safestring] = self.connection.encoders[str] connection_created.send(sender=self.__class__, connection=self) cursor = self.connection.cursor() if new_connection: # sql_auto_is_null in mysql controls whether auto_increment column # on recently-inserted row return when field tested # null. disabling value brings aspect of mysql in line # sql standards. cursor.execute('set sql_auto_is_null = 0') return cursorwrapper(cursor)
so new cursor not created. if call _cursor() had been made, used cursor have been returned.
in threaded model, means multiple threads possibly sharing same database cursor, seems no-no.
there other signs indicate threading not allowed in django. module-level code django/db/init.py, example:
def close_connection(**kwargs): conn in connections.all(): conn.close() signals.request_finished.connect(close_connection)
so if request finished, database connections closed. if there concurrent requests?
seems lot of stuff being shared, indicates threading not allowed. didn't see synchronization code anywhere.
thanks!
i looking through code base, particularly database connectivity parts, , came accross issue. first, 1 gets cursor database using following stamentent: connection module attribute, in threaded model, threads share variable. sounds bit strange. diving in further, cursor() method belongs django.db.backends.basedatabasewrapper , looks this: the key call _cursor(), executes backend code whatever database being used. in case of mysql, executes _cursor() method on django.db.backends.mysql.databasewrapper, looks like: so new cursor not created. if call _cursor() had been made, used cursor have been returned. in threaded model, means multiple threads possibly sharing same database cursor, seems no-no. there other signs indicate threading not allowed in django. module-level code django/db/init.py, example: so if request finished, database connections closed. if there concurrent requests? seems lot of stuff being shared, indicates threading not allowed. didn't see synchronization code anywhere. thanks! | |||||||||
|