test_module.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import sys
  2. import joblib
  3. from joblib.test.common import with_multiprocessing
  4. from joblib.testing import check_subprocess_call
  5. def test_version():
  6. assert hasattr(joblib, "__version__"), (
  7. "There are no __version__ argument on the joblib module"
  8. )
  9. @with_multiprocessing
  10. def test_no_start_method_side_effect_on_import():
  11. # check that importing joblib does not implicitly set the global
  12. # start_method for multiprocessing.
  13. code = """if True:
  14. import joblib
  15. import multiprocessing as mp
  16. # The following line would raise RuntimeError if the
  17. # start_method is already set.
  18. mp.set_start_method("loky")
  19. """
  20. check_subprocess_call([sys.executable, "-c", code])
  21. @with_multiprocessing
  22. def test_no_semaphore_tracker_on_import():
  23. # check that importing joblib does not implicitly spawn a resource tracker
  24. # or a semaphore tracker
  25. code = """if True:
  26. import joblib
  27. from multiprocessing import semaphore_tracker
  28. # The following line would raise RuntimeError if the
  29. # start_method is already set.
  30. msg = "multiprocessing.semaphore_tracker has been spawned on import"
  31. assert semaphore_tracker._semaphore_tracker._fd is None, msg"""
  32. if sys.version_info >= (3, 8):
  33. # semaphore_tracker was renamed in Python 3.8:
  34. code = code.replace("semaphore_tracker", "resource_tracker")
  35. check_subprocess_call([sys.executable, "-c", code])
  36. @with_multiprocessing
  37. def test_no_resource_tracker_on_import():
  38. code = """if True:
  39. import joblib
  40. from joblib.externals.loky.backend import resource_tracker
  41. # The following line would raise RuntimeError if the
  42. # start_method is already set.
  43. msg = "loky.resource_tracker has been spawned on import"
  44. assert resource_tracker._resource_tracker._fd is None, msg
  45. """
  46. check_subprocess_call([sys.executable, "-c", code])