[Python-checkins] cpython: asyncio: document add_signal_handler/remove_signal_handler, add an example for

victor.stinner python-checkins at python.org
Mon Jan 27 10:08:05 CET 2014


http://hg.python.org/cpython/rev/b128387df61d
changeset:   88768:b128387df61d
parent:      88766:b3dd6fc2971c
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Jan 27 10:07:50 2014 +0100
summary:
  asyncio: document add_signal_handler/remove_signal_handler, add an example for
signals

files:
  Doc/library/asyncio-eventloop.rst |  47 +++++++++++++++++++
  1 files changed, 47 insertions(+), 0 deletions(-)


diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst
--- a/Doc/library/asyncio-eventloop.rst
+++ b/Doc/library/asyncio-eventloop.rst
@@ -325,6 +325,29 @@
    This method returns a :ref:`coroutine object <coroutine>`.
 
 
+UNIX signals
+------------
+
+Availability: UNIX only.
+
+.. method:: BaseEventLoop.add_signal_handler(signum, callback, \*args)
+
+   Add a handler for a signal.
+
+   Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
+   Raise :exc:`RuntimeError` if there is a problem setting up the handler.
+
+.. method:: BaseEventLoop.remove_signal_handler(sig)
+
+   Remove a handler for a signal.
+
+   Return ``True`` if a signal handler was removed, ``False`` if not.
+
+.. seealso::
+
+   The :mod:`signal` module.
+
+
 Executor
 --------
 
@@ -381,3 +404,27 @@
 
    :ref:`Hello World example using a coroutine <asyncio-hello-world-coroutine>`.
 
+
+Example: Set signal handlers for SIGINT and SIGTERM
+---------------------------------------------------
+
+Register handlers for signals :py:data:`SIGINT` and :py:data:`SIGTERM`::
+
+    import asyncio
+    import functools
+    import os
+    import signal
+
+    def ask_exit(signame):
+        print("got signal %s: exit" % signame)
+        loop.stop()
+
+    loop = asyncio.get_event_loop()
+    for signame in ('SIGINT', 'SIGTERM'):
+        loop.add_signal_handler(getattr(signal, signame),
+                                functools.partial(ask_exit, signame))
+
+    print("Event loop running forever, press CTRL+c to interrupt.")
+    print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
+    loop.run_forever()
+

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list