_atfork.py 759 B

123456789101112131415161718192021222324252627282930
  1. import sys
  2. __all__ = ['register_after_fork']
  3. if sys.platform == 'win32':
  4. import multiprocessing.util as _util
  5. def _register(func):
  6. def wrapper(arg):
  7. func()
  8. _util.register_after_fork(_register, wrapper)
  9. else:
  10. import os
  11. def _register(func):
  12. os.register_at_fork(after_in_child=func)
  13. def register_after_fork(func):
  14. """Register a callable to be executed in the child process after a fork.
  15. Note:
  16. In python < 3.7 this will only work with processes created using the
  17. ``multiprocessing`` module. In python >= 3.7 it also works with
  18. ``os.fork()``.
  19. Args:
  20. func (function): Function taking no arguments to be called in the child after fork
  21. """
  22. _register(func)