|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+import queue
|
|
|
2
|
+import threading
|
|
|
3
|
+
|
|
1
|
4
|
from django.contrib.auth.models import AnonymousUser
|
|
|
5
|
+from django.db import connection
|
|
2
|
6
|
from django.contrib.messages.storage.fallback import FallbackStorage
|
|
3
|
7
|
from django.contrib.sessions.backends.db import SessionStore
|
|
4
|
8
|
from django.core.signing import Signer
|
|
|
@@ -26,3 +30,35 @@ class RequestFactory(BaseRequestFactory):
|
|
26
|
30
|
request.cookies_to_delete = []
|
|
27
|
31
|
|
|
28
|
32
|
return request
|
|
|
33
|
+
|
|
|
34
|
+
|
|
|
35
|
+def run_concurrently(fn, kwargs=None, num_threads=5):
|
|
|
36
|
+ exceptions = queue.Queue()
|
|
|
37
|
+
|
|
|
38
|
+ def worker(**kwargs):
|
|
|
39
|
+ try:
|
|
|
40
|
+ fn(**kwargs)
|
|
|
41
|
+ except Exception as exc:
|
|
|
42
|
+ exceptions.put(exc)
|
|
|
43
|
+ else:
|
|
|
44
|
+ exceptions.put(None)
|
|
|
45
|
+ finally:
|
|
|
46
|
+ connection.close()
|
|
|
47
|
+
|
|
|
48
|
+ kwargs = kwargs if kwargs is not None else {}
|
|
|
49
|
+
|
|
|
50
|
+ # Run them
|
|
|
51
|
+ threads = [
|
|
|
52
|
+ threading.Thread(target=worker, name='thread-%d' % i, kwargs=kwargs)
|
|
|
53
|
+ for i in range(num_threads)
|
|
|
54
|
+ ]
|
|
|
55
|
+ try:
|
|
|
56
|
+ for thread in threads:
|
|
|
57
|
+ thread.start()
|
|
|
58
|
+ finally:
|
|
|
59
|
+ for thread in threads:
|
|
|
60
|
+ thread.join()
|
|
|
61
|
+
|
|
|
62
|
+ # Retrieve exceptions
|
|
|
63
|
+ exceptions = [exceptions.get(block=False) for i in range(num_threads)]
|
|
|
64
|
+ return [exc for exc in exceptions if exc is not None]
|