-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.py
41 lines (33 loc) · 990 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import queue
import threading
from django.db import connection
def run_concurrently(fn, kwargs=None, num_threads=5):
"""
This function copy-pasted from `oscar.tests.utils`
until it will be available for import.
"""
exceptions = queue.Queue()
def worker(**kwargs):
try:
fn(**kwargs)
except Exception as exc:
exceptions.put(exc)
else:
exceptions.put(None)
finally:
connection.close()
kwargs = kwargs if kwargs is not None else {}
# Run them
threads = [
threading.Thread(target=worker, name='thread-%d' % i, kwargs=kwargs)
for i in range(num_threads)
]
try:
for thread in threads:
thread.start()
finally:
for thread in threads:
thread.join()
# Retrieve exceptions
exceptions = [exceptions.get(block=False) for i in range(num_threads)]
return [exc for exc in exceptions if exc is not None]