=========
multiexit
=========
A better, saner and more useful atexit_ replacement for Python 3 that supports
multiprocessing_.
Inspired by the following StackOverflow question and experience on building
multiprocessing daemons:
https://stackoverflow.com/q/2546276
.. _atexit: https://docs.python.org/3/library/atexit.html
.. _multiprocessing: https://docs.python.org/3/library/multiprocessing.html
``multiexit`` will install a handler for the SIGTERM and SIGINT signals and
execute the registered exit functions in *LIFO* order (Last In First Out).
Exit functions can be registered so that only the calling process will call
them (the default), or as *shared* exit functions that will be called by the
calling process and all the children subprocesses that inherit it.
Install
=======
``multiexit`` is available for Python 3 from PyPI_.
.. _PyPI: https://pypi.python.org/pypi/multiexit/
.. code-block:: sh
pip3 install multiexit
API
===
On the main process, before forking or creating any subprocess, call
``multiexit.install``:
.. code-block:: python
def install(
signals=(signal.SIGTERM, signal.SIGINT),
except_hook=True,
)
:signals:
Signals to install handler. Usually only ``SIGTERM`` and ``SIGINT`` are
required.
:except_hook:
Also install a `sys.excepthook`_ that will call the exit functions in case of
an unexpected exception.
.. _`sys.excepthook`: https://docs.python.org/3/library/sys.html#sys.excepthook
Then, for each exit function, on any subprocess, call ``multiexit.register``:
.. code-block:: python
register(func, shared=False)
:func:
Exit function to register. Any callable without arguments.
:shared:
If ``shared``, the exit function will be called by the calling process but
also by all the children subprocesses that inherit it (thus the ones
created after registering it).
If ``shared`` is ``False``, the default, only the calling process will execute
the exit function.
Example
=======
.. code-block:: python
from time import sleep
from signal import SIGTERM
from os import kill, getpid
from multiprocessing import Process
from multiexit import install, register, unregister
if __name__ == '__main__':
# Always call install() on the main process before creating any
# subprocess
#
# This installs a handler for SIGTERM and SIGINT. Subprocesses will
# inherit this handler. It also assigns the current PID as the master
# process, which will allow to choose between exit or os._exit calls
# when quitting.
install()
def _subproc1():
@register
def subproc1_clean():
print('Subprocess clean!')
sleep(1000)
# Register shared exit function so all subprocess call this
def shared_exit():
print('Shared exit being called by {} ...'.format(getpid()))
register(shared_exit, shared=True)
subproc1 = Process(
name='SubProcess1',
target=_subproc1,
)
# proc.daemon = True
# daemon means that signals (like SIGTERM) will be propagated automatically
# to children. Set to false (the default), to handle the SIGTERM
# (process.terminate()) to the children yourself.
subproc1.start()
# Register a cleaner using a decorator
@register
def clean_main():
print('Terminating child {}'.format(
subproc1.pid,
))
subproc1.terminate()
subproc1.join()
print('Child {} ended with {}'.format(
subproc1.pid,
subproc1.exitcode,
))
# Wait, and then kill main process
sleep(3)
# Suicide
kill(getpid(), SIGTERM)
For a more extensive example check out ``example.py``.
Changes
=======
1.5.0
-----
A ``SIGINT`` handler is now installed by default to handle Ctrl+C. This
means that Python's ``signal.default_int_handler`` is overridden, thus
Ctrl+C no longer raises ``KeyboardInterrupt``. If this behavior is
undesired, pass ``signals=(signal.SIGTERM)`` when calling ``install()``.
Ctrl+C sends ``SIGINT`` to all processes in the terminal's foreground
process group, so in order to behave like a ``SIGTERM`` shutdown flow, which
is sent only on the parent process, the handler will ignore ``SIGINT`` on
the children to allow the parent to decide what to do with the subprocesses
and their corresponding shutdown sequence.
This fixes issue #3
License
=======
::
Copyright (C) 2018-2019 KuraLabs S.R.L
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Raw data
{
"_id": null,
"home_page": "https://github.com/kuralabs/multiexit",
"name": "multiexit",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "multiexit multiprocessing atexit",
"author": "KuraLabs S.R.L",
"author_email": "info@kuralabs.io",
"download_url": "",
"platform": "",
"description": "=========\nmultiexit\n=========\n\nA better, saner and more useful atexit_ replacement for Python 3 that supports\nmultiprocessing_.\n\nInspired by the following StackOverflow question and experience on building\nmultiprocessing daemons:\n\nhttps://stackoverflow.com/q/2546276\n\n.. _atexit: https://docs.python.org/3/library/atexit.html\n.. _multiprocessing: https://docs.python.org/3/library/multiprocessing.html\n\n``multiexit`` will install a handler for the SIGTERM and SIGINT signals and\nexecute the registered exit functions in *LIFO* order (Last In First Out).\n\nExit functions can be registered so that only the calling process will call\nthem (the default), or as *shared* exit functions that will be called by the\ncalling process and all the children subprocesses that inherit it.\n\n\nInstall\n=======\n\n``multiexit`` is available for Python 3 from PyPI_.\n\n.. _PyPI: https://pypi.python.org/pypi/multiexit/\n\n.. code-block:: sh\n\n pip3 install multiexit\n\n\nAPI\n===\n\nOn the main process, before forking or creating any subprocess, call\n``multiexit.install``:\n\n.. code-block:: python\n\n def install(\n signals=(signal.SIGTERM, signal.SIGINT),\n except_hook=True,\n )\n\n:signals:\n Signals to install handler. Usually only ``SIGTERM`` and ``SIGINT`` are\n required.\n\n:except_hook:\n Also install a `sys.excepthook`_ that will call the exit functions in case of\n an unexpected exception.\n\n.. _`sys.excepthook`: https://docs.python.org/3/library/sys.html#sys.excepthook\n\nThen, for each exit function, on any subprocess, call ``multiexit.register``:\n\n.. code-block:: python\n\n register(func, shared=False)\n\n:func:\n Exit function to register. Any callable without arguments.\n\n:shared:\n If ``shared``, the exit function will be called by the calling process but\n also by all the children subprocesses that inherit it (thus the ones\n created after registering it).\n If ``shared`` is ``False``, the default, only the calling process will execute\n the exit function.\n\n\nExample\n=======\n\n.. code-block:: python\n\n from time import sleep\n from signal import SIGTERM\n from os import kill, getpid\n from multiprocessing import Process\n\n from multiexit import install, register, unregister\n\n\n if __name__ == '__main__':\n\n # Always call install() on the main process before creating any\n # subprocess\n #\n # This installs a handler for SIGTERM and SIGINT. Subprocesses will\n # inherit this handler. It also assigns the current PID as the master\n # process, which will allow to choose between exit or os._exit calls\n # when quitting.\n install()\n\n def _subproc1():\n\n @register\n def subproc1_clean():\n print('Subprocess clean!')\n\n sleep(1000)\n\n # Register shared exit function so all subprocess call this\n def shared_exit():\n print('Shared exit being called by {} ...'.format(getpid()))\n\n register(shared_exit, shared=True)\n\n subproc1 = Process(\n name='SubProcess1',\n target=_subproc1,\n )\n # proc.daemon = True\n # daemon means that signals (like SIGTERM) will be propagated automatically\n # to children. Set to false (the default), to handle the SIGTERM\n # (process.terminate()) to the children yourself.\n subproc1.start()\n\n # Register a cleaner using a decorator\n @register\n def clean_main():\n print('Terminating child {}'.format(\n subproc1.pid,\n ))\n subproc1.terminate()\n subproc1.join()\n print('Child {} ended with {}'.format(\n subproc1.pid,\n subproc1.exitcode,\n ))\n\n # Wait, and then kill main process\n sleep(3)\n\n # Suicide\n kill(getpid(), SIGTERM)\n\nFor a more extensive example check out ``example.py``.\n\n\nChanges\n=======\n\n1.5.0\n-----\n\nA ``SIGINT`` handler is now installed by default to handle Ctrl+C. This\nmeans that Python's ``signal.default_int_handler`` is overridden, thus\nCtrl+C no longer raises ``KeyboardInterrupt``. If this behavior is\nundesired, pass ``signals=(signal.SIGTERM)`` when calling ``install()``.\n\nCtrl+C sends ``SIGINT`` to all processes in the terminal's foreground\nprocess group, so in order to behave like a ``SIGTERM`` shutdown flow, which\nis sent only on the parent process, the handler will ignore ``SIGINT`` on\nthe children to allow the parent to decide what to do with the subprocesses\nand their corresponding shutdown sequence.\n\nThis fixes issue #3\n\n\nLicense\n=======\n\n::\n\n Copyright (C) 2018-2019 KuraLabs S.R.L\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing,\n software distributed under the License is distributed on an\n \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n KIND, either express or implied. See the License for the\n specific language governing permissions and limitations\n under the License.\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "atexit replacement that supports multiprocessing",
"version": "1.5.0",
"project_urls": {
"Homepage": "https://github.com/kuralabs/multiexit"
},
"split_keywords": [
"multiexit",
"multiprocessing",
"atexit"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9c8b5e74b740675eee47792c4e2cae0815427820395e54992548d8fc99c90656",
"md5": "1fd63a5fe24dacf9a2977b1731fc3427",
"sha256": "aa773f041e917a536ad58834bef7e08e6bcd1af2d09d9cd1ea8e5ac3719fcfed"
},
"downloads": -1,
"filename": "multiexit-1.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1fd63a5fe24dacf9a2977b1731fc3427",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9682,
"upload_time": "2019-10-22T23:59:54",
"upload_time_iso_8601": "2019-10-22T23:59:54.107100Z",
"url": "https://files.pythonhosted.org/packages/9c/8b/5e74b740675eee47792c4e2cae0815427820395e54992548d8fc99c90656/multiexit-1.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-10-22 23:59:54",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kuralabs",
"github_project": "multiexit",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "multiexit"
}