• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# mypy: allow-untyped-defs
2import sys
3
4
5__all__ = ["register_after_fork"]
6
7if sys.platform == "win32":
8    import multiprocessing.util as _util
9
10    def _register(func):
11        def wrapper(arg):
12            func()
13
14        _util.register_after_fork(_register, wrapper)
15
16else:
17    import os
18
19    def _register(func):
20        os.register_at_fork(after_in_child=func)
21
22
23def register_after_fork(func):
24    """Register a callable to be executed in the child process after a fork.
25
26    Note:
27        In python < 3.7 this will only work with processes created using the
28        ``multiprocessing`` module. In python >= 3.7 it also works with
29        ``os.fork()``.
30
31    Args:
32        func (function): Function taking no arguments to be called in the child after fork
33
34    """
35    _register(func)
36