Skip to content
Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env python
import os
import logging
from threading import Thread
from import BaseCommand, CommandError
from django.conf import settings
from optparse import OptionParser
# testing with postgresql 9.1
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 's3cr3t',
'HOST': '',
'PORT': '',
from django.db import transaction
from django.contrib.auth.models import User
class MyThread(Thread):
def __init__(self, username):
super(MyThread, self).__init__(name=username)
self.username = username
def run(self):
# passes with get_or_create
instance, created = User.objects.get_or_create(username=self.username)
# fails with try get .. except create
#created = False
#instance = User.objects.get(username=self.username)
#except User.DoesNotExist:
#instance = User.objects.create(username=self.username)
#created = False
print '%s %s' % (, created)
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("--num-threads", type="int", default=5, dest="threads")
options, args = parser.parse_args()
for i in range(options.threads):
thread = MyThread(username="test_user")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.