Skip to main content

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!

can django run in threaded model? - stack overflow

learn, share, build

each month, on 50 million developers come stack overflow learn, share knowledge, , build careers.

join world’s largest developer community.

sign up

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!

share|improve question
    
why want use threading? what's particular use case? normal way achieve parallel requests configure server run in multi-process or multi-threaded way. trying couldn't done way? – daniel roseman jul 7 '12 @ 14:51
    
that's precisely question. can see 1 process handling 1 request, since different processes share nothing, in case of threads, fail understand how issues above wouldn't create problems. – alexandre jul 7 '12 @ 19:30

your answer

 
discard

posting answer, agree privacy policy , terms of service.

browse other questions tagged or ask own question.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -