concurrency.py 978 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. """
  4. taken from django-oscar
  5. 用来测试一些并发的情况
  6. """
  7. try:
  8. import queue
  9. except ImportError:
  10. import Queue as queue
  11. import threading
  12. def run_concurrently(fn, kwargs=None, num_threads=5):
  13. exceptions = queue.Queue()
  14. def worker(**kwargs):
  15. try:
  16. result = fn(**kwargs)
  17. except Exception as exc:
  18. exceptions.put(exc)
  19. else:
  20. exceptions.put(result)
  21. kwargs = kwargs if kwargs is not None else {}
  22. # Run them
  23. threads = [
  24. threading.Thread(target=worker, name='thread-%d' % i, kwargs=kwargs)
  25. for i in range(num_threads)
  26. ]
  27. try:
  28. for thread in threads:
  29. thread.start()
  30. finally:
  31. for thread in threads:
  32. thread.join()
  33. # Retrieve exceptions
  34. exceptions = [exceptions.get(block=False) for _ in range(num_threads)]
  35. return exceptions