conftest.py 829 B

1234567891011121314151617181920212223242526272829303132
  1. import gc
  2. from contextlib import suppress
  3. import multiprocessing as mp
  4. import psutil
  5. import pytest
  6. from hivemind.utils.mpfuture import MPFuture, SharedBytes
  7. from hivemind.utils.logging import get_logger
  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()