conftest.py 905 B

123456789101112131415161718192021222324252627282930313233
  1. import gc
  2. import multiprocessing as mp
  3. from contextlib import suppress
  4. import psutil
  5. import pytest
  6. from hivemind.utils.logging import get_logger
  7. from hivemind.utils.mpfuture import MPFuture
  8. logger = get_logger(__name__)
  9. @pytest.fixture(autouse=True, scope="session")
  10. def cleanup_children():
  11. yield
  12. gc.collect() # Call .__del__() for removed objects
  13. children = psutil.Process().children(recursive=True)
  14. if children:
  15. logger.info(f"Cleaning up {len(children)} leftover child processes")
  16. for child in children:
  17. with suppress(psutil.NoSuchProcess):
  18. child.terminate()
  19. psutil.wait_procs(children, timeout=1)
  20. for child in children:
  21. with suppress(psutil.NoSuchProcess):
  22. child.kill()
  23. # Broken code or killing of child processes may leave the MPFuture backend corrupted
  24. MPFuture.reset_backend()