Here are the examples of the python api django.db.django_db_connections.all taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
3 Examples
3
Example 1
Project: SmartElect Source File: sending.py
def _create_pool(self):
"""
Create a multiprocessing worker pool for sending bulk messages.
"""
# close database connections manually to make sure they're not passed
# to subprocesses
logger.debug('Closing database connections before starting worker pool.')
for conn in django_db_connections.all():
conn.close()
cache.close()
# send the messages in parallel using ``concurrent_workers`` processes. use
# multiprocessing rather than threads so we don't have to clean
# up database connections that the threads might leave open.
logger.debug('Starting worker pool with {0} processes.'.format(self.concurrent_workers))
return multiprocessing.Pool(processes=self.concurrent_workers,
maxtasksperchild=MAX_MESSAGES_PER_CHILD)
0
Example 2
Project: SmartElect Source File: test_sending.py
def test_send_is_threadsafe(self):
"""
Ensure send_message_by_id does not accidentally send more than one
SMS to the same user if it happens to be running more than once with the
same ID.
"""
trigger = threading.Event()
def target(pk):
# wait until all threads have started before attempting to send
trigger.wait()
send_message_by_id(pk)
# workaround https://code.djangoproject.com/ticket/22420
for conn in django_db_connections.all():
conn.close()
m = BulkMessageFactory(batch=self.batch, sms=None)
threads = [threading.Thread(target=target, args=(m.pk,))
for i in range(10)]
for t in threads:
t.start()
# wake up all threads at the same time
trigger.set()
for t in threads:
t.join()
# only 1 message should be sent out in total
self.assertEqual(len(self.outbound), 1)
0
Example 3
Project: SmartElect Source File: test_sending.py
def test_bulk_sms_send_multiproc(self):
# close database connections manually to make sure they're not passed
# to subprocesses
for conn in django_db_connections.all():
conn.close()
# send the messages in parallel using 10 processes. use
# multiprocessing rather than threads so we don't have to clean
# up database connections that the threads might leave open.
cache.close()
pool = multiprocessing.Pool(10)
try:
for i in range(30):
BulkMessageFactory(batch=self.batch, sms=None)
for j in range(10):
BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
# 40 is the first multiple of 10 greater than or equal to 31
num_sent = send_messages(self.batch, num_to_send=40, map_=pool.map)
batch = Batch.objects.get(pk=self.batch.pk)
self.assertEqual(batch.errors, 0)
self.assertEqual(batch.status, Batch.COMPLETED)
# assert that we report the right number sent too
self.assertEqual(num_sent, 30)
self.assertEqual(len(self.outbound), 30)
finally:
pool.terminate()
pool.join()