Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6afd11c7
Commit
6afd11c7
authored
Mar 31, 2012
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #14456: improve documentation of the signal module w.r.t. threads.
parent
f70401e8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
40 deletions
+52
-40
Doc/library/signal.rst
Doc/library/signal.rst
+52
-40
No files found.
Doc/library/signal.rst
View file @
6afd11c7
...
@@ -5,46 +5,58 @@
...
@@ -5,46 +5,58 @@
:synopsis: Set handlers for asynchronous events.
:synopsis: Set handlers for asynchronous events.
This module provides mechanisms to use signal handlers in Python. Some general
This module provides mechanisms to use signal handlers in Python.
rules for working with signals and their handlers:
* A handler for a particular signal, once set, remains installed until it is
General rules
explicitly reset (Python emulates the BSD style interface regardless of the
-------------
underlying implementation), with the exception of the handler for
:const:`SIGCHLD`, which follows the underlying implementation.
The :func:`signal.signal` function allows to define custom handlers to be
executed when a signal is received. A small number of default handlers are
* There is no way to "block" signals temporarily from critical sections (since
installed: :const:`SIGPIPE` is ignored (so write errors on pipes and sockets
this is not supported by all Unix flavors).
can be reported as ordinary Python exceptions) and :const:`SIGINT` is
translated into a :exc:`KeyboardInterrupt` exception.
* Although Python signal handlers are called asynchronously as far as the Python
user is concerned, they can only occur between the "atomic" instructions of the
A handler for a particular signal, once set, remains installed until it is
Python interpreter. This means that signals arriving during long calculations
explicitly reset (Python emulates the BSD style interface regardless of the
implemented purely in C (such as regular expression matches on large bodies of
underlying implementation), with the exception of the handler for
text) may be delayed for an arbitrary amount of time.
:const:`SIGCHLD`, which follows the underlying implementation.
* When a signal arrives during an I/O operation, it is possible that the I/O
There is no way to "block" signals temporarily from critical sections (since
operation raises an exception after the signal handler returns. This is
this is not supported by all Unix flavors).
dependent on the underlying Unix system's semantics regarding interrupted system
calls.
Execution of Python signal handlers
* Because the C signal handler always returns, it makes little sense to catch
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
synchronous errors like :const:`SIGFPE` or :const:`SIGSEGV`.
A Python signal handler does not get executed inside the low-level (C) signal
* Python installs a small number of signal handlers by default: :const:`SIGPIPE`
handler. Instead, the low-level signal handler sets a flag which tells the
is ignored (so write errors on pipes and sockets can be reported as ordinary
:term:`virtual machine` to execute the corresponding Python signal handler
Python exceptions) and :const:`SIGINT` is translated into a
at a later point(for example at the next :term:`bytecode` instruction).
:exc:`KeyboardInterrupt` exception. All of these can be overridden.
This has consequences:
* Some care must be taken if both signals and threads are used in the same
* It makes little sense to catch synchronous errors like :const:`SIGFPE` or
program. The fundamental thing to remember in using signals and threads
:const:`SIGSEGV`.
simultaneously is: always perform :func:`signal` operations in the main thread
of execution. Any thread can perform an :func:`alarm`, :func:`getsignal`,
* A long-running calculation implemented purely in C (such as regular
:func:`pause`, :func:`setitimer` or :func:`getitimer`; only the main thread
expression matching on a large body of text) may run uninterrupted for an
can set a new signal handler, and the main thread will be the only one to
arbitrary amount of time, regardless of any signals received. The Python
receive signals (this is enforced by the Python :mod:`signal` module, even
signal handlers will be called when the calculation finishes.
if the underlying thread implementation supports sending signals to
individual threads). This means that signals can't be used as a means of
inter-thread communication. Use locks instead.
Signals and threads
^^^^^^^^^^^^^^^^^^^
Python signal handlers are always executed in the main Python thread,
even if the signal was received in another thread. This means that signals
can't be used as a means of inter-thread communication. You can use
the synchronization primitives from the :mod:`threading` module instead.
Besides, only the main thread is allowed to set a new signal handler.
Module contents
---------------
The variables defined in the :mod:`signal` module are:
The variables defined in the :mod:`signal` module are:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment