<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bottiglie di Leida &#187; segnali</title>
	<atom:link href="http://www.bottigliedileida.net/etichetta/segnali/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bottigliedileida.net</link>
	<description>Un blog sulla scienza e gli scienziati</description>
	<lastBuildDate>Tue, 15 Nov 2011 13:47:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Come catturare un segnale Unix con Python</title>
		<link>http://www.bottigliedileida.net/2009/09/come-catturare-un-segnale-di-sistema-con-python/</link>
		<comments>http://www.bottigliedileida.net/2009/09/come-catturare-un-segnale-di-sistema-con-python/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 08:54:58 +0000</pubDate>
		<dc:creator>Jacopo</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[segnali]]></category>

		<guid isPermaLink="false">http://bottigliedileida.wordpress.com/?p=174</guid>
		<description><![CDATA[Unix Signals, to be short, are various notifications sent to a process in order to notify it of various &#8220;important&#8221; events. By their nature, they interrupt whatever the process is doing at this minute, and force it to handle them immediately. Each signal has an integer number that represents it (1, 2 and so on), [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<p style="text-align: justify;"><strong><em>Unix Signals</em></strong>, to be short, <strong>are various notifications sent to a process in order to notify it of various &#8220;important&#8221; events</strong>. By their nature, they interrupt whatever the process is doing at this minute, and force it to handle them immediately. Each signal has an integer number that represents it (1, 2 and so on), as well as a symbolic name that is usually defined in the file /usr/include/signal.h</p>
<p style="text-align: right;">[via | <a href="http://users.actcom.co.il/~choo/lupg/tutorials/signals/signals-programming.html" target="_blank">Introduction to Unix Signal Programming</a>]</p>
</blockquote>
<p>Un modo molto semplice per catturare un segnale asincrono in un sistema Unix con Python è il seguente:</p>
<h4>Signal Traps</h4>
<pre class="brush: python; ruler: false;">#! /usr/local/bin/python

import signal import sys

def signal_handler(signal, frame):
        print 'You pressed Ctrl+C!'
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C'
while 1:
        continue</pre>
<p style="text-align: justify;"><strong>signal.SIGINT</strong> indica la costante che identifica il segnale, in questo caso il segnale di <em>Interrupt</em> del processo (mandato, per capirci, quando interrompiamo un processo con <strong>ctrl+c</strong> da shell). Ad esso possiamo sostituire un&#8217;altra costante identificativa. Per una lista dei segnali disponibili è possibile consultare la pagina del manuale di Unix:</p>
<p style="padding-left: 30px;"><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?signal+7" target="_blank">man signal (7)</a></p>
<p style="text-align: justify;">All&#8217;arrivo del segnale l&#8217;handler chiama il metodo signal_handler, il quale in questo caso stampa una stringa ed esce, ma in casi più realistici può effettuare operazioni di freezing del contesto o chiusura e aggiornamento di file o strutture dati. Per ulteriori informazioni sulla gestione dei segnali ci possiamo rifare all&#8217;<a href="http://docs.python.org/library/signal.html" target="_blank">ottima documentazione</a> di Python.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bottigliedileida.net/2009/09/come-catturare-un-segnale-di-sistema-con-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

