{"id":7334,"date":"2020-07-24T13:38:35","date_gmt":"2020-07-24T13:38:35","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7334"},"modified":"2022-08-06T13:15:02","modified_gmt":"2022-08-06T13:15:02","slug":"python-signal","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-signal","title":{"rendered":"Python Signal Module &#8211; What are Signals and How to Create Them?"},"content":{"rendered":"\n<p>In this article, we&#8217;ll take a look at using the Python signal module.<\/p>\n\n\n\n<p>This module is very useful when we want to handle certain signals using Python.<\/p>\n\n\n\n<p>First, let&#8217;s look at what a signal means.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Signal?<\/h2>\n\n\n\n<p>A Signal is a means through which a program can receive information from the Operating System. When the operating system receives certain events, it can pass that to programs in the form of signals.<\/p>\n\n\n\n<p>For example, when we press the keystrokes <code>Ctrl + C<\/code> on our keyboard, the Operating System will generate a signal and pass this on to programs. For this particular combination, the signal <code>SIGINT<\/code> is generated and passed to the programs.<\/p>\n\n\n\n<p>For all common operating systems, there is a standard pattern for assigning these signals, which are generally short for integers.<\/p>\n\n\n\n<p>In Python, these signals are defined in the <code>signal<\/code> module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport signal\n<\/pre><\/div>\n\n\n<p>To look at all the valid signals in your system (depends on the OS), you can use <code>signal.valid_signals()<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport signal\n\nvalid_signals = signal.valid_signals()\n\nprint(valid_signals)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{&lt;Signals.SIGHUP: 1&gt;, &lt;Signals.SIGINT: 2&gt;, &lt;Signals.SIGQUIT: 3&gt;, &lt;Signals.SIGILL: 4&gt;, &lt;Signals.SIGTRAP: 5&gt;, &lt;Signals.SIGABRT: 6&gt;, &lt;Signals.SIGBUS: 7&gt;, &lt;Signals.SIGFPE: 8&gt;, &lt;Signals.SIGKILL: 9&gt;, &lt;Signals.SIGUSR1: 10&gt;, &lt;Signals.SIGSEGV: 11&gt;, &lt;Signals.SIGUSR2: 12&gt;, &lt;Signals.SIGPIPE: 13&gt;, &lt;Signals.SIGALRM: 14&gt;, &lt;Signals.SIGTERM: 15&gt;, 16, &lt;Signals.SIGCHLD: 17&gt;, &lt;Signals.SIGCONT: 18&gt;, &lt;Signals.SIGSTOP: 19&gt;, &lt;Signals.SIGTSTP: 20&gt;, &lt;Signals.SIGTTIN: 21&gt;, &lt;Signals.SIGTTOU: 22&gt;, &lt;Signals.SIGURG: 23&gt;, &lt;Signals.SIGXCPU: 24&gt;, &lt;Signals.SIGXFSZ: 25&gt;, &lt;Signals.SIGVTALRM: 26&gt;, &lt;Signals.SIGPROF: 27&gt;, &lt;Signals.SIGWINCH: 28&gt;, &lt;Signals.SIGIO: 29&gt;, &lt;Signals.SIGPWR: 30&gt;, &lt;Signals.SIGSYS: 31&gt;, &lt;Signals.SIGRTMIN: 34&gt;, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, &lt;Signals.SIGRTMAX: 64&gt;}\n<\/pre><\/div>\n\n\n<p>Now, for all signals, there are some default actions which the OS will assign to every program.<\/p>\n\n\n\n<p>If we want to have some other behavior instead, we can use signal handlers!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python Signal Handler?<\/h2>\n\n\n\n<p>A Signal Handler is a user defined function, where Python signals can be handled.<\/p>\n\n\n\n<p>If we take the signal <code>SIGINT<\/code> (Interrupt Signal), the default behavior would be to stop the current running program.<\/p>\n\n\n\n<p>We can, however, assign a signal handler to detect this signal and do our custom processing instead!<\/p>\n\n\n\n<p>Let&#8217;s take a look at how we can do this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport signal  \nimport time  \n\n# Our signal handler\ndef signal_handler(signum, frame):  \n    print(&quot;Signal Number:&quot;, signum, &quot; Frame: &quot;, frame)  \n\ndef exit_handler(signum, frame):\n    print(&#039;Exiting....&#039;)\n    exit(0)\n\n# Register our signal handler with `SIGINT`(CTRL + C)\nsignal.signal(signal.SIGINT, signal_handler)\n\n# Register the exit handler with `SIGTSTP` (Ctrl + Z)\nsignal.signal(signal.SIGTSTP, exit_handler)\n\n# While Loop\nwhile 1:  \n    print(&quot;Press Ctrl + C&quot;) \n    time.sleep(3) \n<\/pre><\/div>\n\n\n<p>Here, after we run our program, when we press Ctrl + C, the program will go to the <code>signal_handler()<\/code> function, since we have registered the handler with <code>SIGINT<\/code> (Ctrl + C).<\/p>\n\n\n\n<p>We also have another handler <code>exit_handler()<\/code> which exits the program if we press Ctrl + Z, which sends a <code>SIGTSTP<\/code> signal.<\/p>\n\n\n\n<p>Let&#8217;s look at the output<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPress Ctrl + C\n^CSignal Number: 2  Frame:  &lt;frame at 0x7fe62f033640, file &#039;python_signals.py&#039;, line 22, code &lt;module&gt;&gt;\n^ZExiting....\n<\/pre><\/div>\n\n\n<p>Here, I pressed Ctrl + C to go to the <code>signal_handler()<\/code> function, and then pressed Ctrl + Z to exit the program. Notice that there is a stack frame object (<code>frame<\/code>) as well, to track the runtime stack of the main program.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using Alarm Signals<\/h2>\n\n\n\n<p>We can use the <code>SIGALARM<\/code> signal to send alarm signals to our program. Let&#8217;s write a simple signal handler that handles this Python signal.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport signal  \nimport time  \n\ndef alarm_handler(signum, frame):  \n    print(&#039;Alarm at:&#039;, time.ctime())  \n\n# Register the alarm signal with our handler\nsignal.signal(signal.SIGALRM, alarm_handler)\n\nsignal.alarm(3)  # Set the alarm after 3 seconds  \n\nprint(&#039;Current time:&#039;, time.ctime())  \n\ntime.sleep(6)  # Make a sufficient delay for the alarm to happen \n<\/pre><\/div>\n\n\n<p>In the last line, we sleep for sufficient time (6 seconds) to allow the alarm signal to pass to our program. Otherwise, since the program would have terminated, the signal won&#8217;t be received!<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nCurrent time: Thu Jul 23 00:41:40 2020\nAlarm at: Thu Jul 23 00:41:43 2020\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned about how we can set up signal handlers to handle various signals, using the <code>signal<\/code> module. Visit the link here to learn more about <a href=\"https:\/\/www.askpython.com\/python-modules\" class=\"rank-math-link\">Python modules<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python signal module <a href=\"https:\/\/docs.python.org\/3\/library\/signal.html\" target=\"_blank\" rel=\"noopener\">Documentation<\/a><\/li><li>JournalDev article on Python Signals<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll take a look at using the Python signal module. This module is very useful when we want to handle certain signals using Python. First, let&#8217;s look at what a signal means. What is a Signal? A Signal is a means through which a program can receive information from the Operating System. [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":7338,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7334"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7338"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}