conftest.py 720 B

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