conftest.py 828 B

12345678910111213141516171819202122232425262728293031
  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, SharedBytes
  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. MPFuture.reset_backend()