Salt relies on this custom implementation of :py:class:`~multiprocessing.Process` to simplify/automate some common procedures, for example, logging in the new process is configured for "free" for every new process. This is most important in platforms which default to ``spawn` instea
| 835 | |
| 836 | |
| 837 | class Process(multiprocessing.Process): |
| 838 | """ |
| 839 | Salt relies on this custom implementation of :py:class:`~multiprocessing.Process` to |
| 840 | simplify/automate some common procedures, for example, logging in the new process is |
| 841 | configured for "free" for every new process. |
| 842 | This is most important in platforms which default to ``spawn` instead of ``fork`` for |
| 843 | new processes. |
| 844 | |
| 845 | This is achieved by some dunder methods in the class: |
| 846 | |
| 847 | * ``__new__``: |
| 848 | |
| 849 | This method ensures that any arguments and/or keyword arguments that are passed to |
| 850 | ``__init__`` are captured. |
| 851 | |
| 852 | By having this information captured, we can define ``__setstate__`` and ``__getstate__`` |
| 853 | to automatically take care of reconstructing the object state on spawned processes. |
| 854 | |
| 855 | * ``__getstate__``: |
| 856 | |
| 857 | This method should return a dictionary which will be used as the ``state`` argument to |
| 858 | :py:method:`salt.utils.process.Process.__setstate__`. |
| 859 | Usually, when subclassing, this method does not need to be implemented, however, |
| 860 | if implemented, `super()` **must** be called. |
| 861 | |
| 862 | * ``__setstate__``: |
| 863 | |
| 864 | This method reconstructs the object on the spawned process. |
| 865 | The ``state`` argument is constructed by the |
| 866 | :py:method:`salt.utils.process.Process.__getstate__` method. |
| 867 | Usually, when subclassing, this method does not need to be implemented, however, |
| 868 | if implemented, `super()` **must** be called. |
| 869 | |
| 870 | |
| 871 | An example of where ``__setstate__`` and ``__getstate__`` needed to be subclassed can be |
| 872 | seen in :py:class:`salt.master.MWorker`. |
| 873 | |
| 874 | The gist of it is something like, if there are internal attributes which need to maintain |
| 875 | their state on spawned processes, then, subclasses must implement ``__getstate__`` and |
| 876 | ``__setstate__`` to ensure that. |
| 877 | |
| 878 | |
| 879 | For example: |
| 880 | |
| 881 | |
| 882 | .. code-block:: python |
| 883 | |
| 884 | import salt.utils.process |
| 885 | |
| 886 | class MyCustomProcess(salt.utils.process.Process): |
| 887 | |
| 888 | def __init__(self, opts, **kwargs): |
| 889 | super().__init__(**kwargs) |
| 890 | self.opts = opts |
| 891 | |
| 892 | # This attribute, counter, should only start at 0 on the initial(parent) process. |
| 893 | # Any child processes, need to carry the current value of the counter(instead of |
| 894 | # starting at zero). |
no outgoing calls
no test coverage detected