Pythonã§UNIXã·ã°ãã«ã®ãã³ãã©ãæ±ã(åå)
Pythonã¹ã¯ãªããã®å®è¡ä¸ã«Ctrl+Cãæ¼ãã¨
Traceback (most recent call last): File "[ã¹ã¯ãªããã®å ´æ]", line xxx, in <module> app.main() File "[ã¹ã¯ãªããã®å ´æ]", line xxx, in main gtk.main() KeyboardInterrupt
ã®ããã«KeyboardInterruptä¾å¤ãçºçãã¦ã¡ãã»ã¼ã¸ã表示ãããPyGTKãç¨ããGUIã¢ããªã±ã¼ã·ã§ã³ã§ããã®ããã«ã¡ãã»ã¼ã¸ã表示ããããCè¨èªãValaè¨èªãªã©ã§GTK+ãç¨ããããã°ã©ã ãä½æããå ´åã«ã¯ãã®ããã«ã¯ãªããªãããPyGTKã§ãã®ã¡ãã»ã¼ã¸ãåºãã®ãéªéãªå ´åãæå¶ãããã¨ã¯ã§ããã
Pythonã«ãããUNIXã·ã°ãã«ã®å¦ç
ä¸è¬çã«ãGNU/Linuxä¸ã®ã¢ããªã±ã¼ã·ã§ã³ã¯ãUNIXã·ã°ãã«ãåãåã£ãã¨ãã«ä»»æã®ãã³ãã©é¢æ°ãå¼ã³åºãããã«é¢é£ä»ãããã¨ãã§ããã
ããã¯OSã®æ©è½(ã·ã¹ãã ã³ã¼ã«)ã®signal()ã«ã·ã°ãã«ã®èå¥çªå·ã¨ãã³ãã©é¢æ°ã®å ´æãæå®ãããã¨ã§è¡ãããPythonã§ã¯signalã¨ããã¢ã¸ã¥ã¼ã«ã®signal.signal()ãç¨ãã¦åæ§ã®è¦é ã§ãã³ãã©ãæå®ã§ããã
ãã³ãã©ã¯2ã¤ã®å¼æ°ãåãåãããã«ãã¦ããã1ã¤ç®ã®å¼æ°ã«ã·ã°ãã«ã®èå¥å¤ãå
¥ãã
# ã·ã°ãã«SIGINT(Ctrl+Cãªã©ã§çºç)ãåãåã£ãã¨ãã«é¢æ°func()ãå¼ã¶ signal.signal(signal.SIGINT, func) ... def func(num, frame): """ UNIXã·ã°ãã«ã®ãã³ãã© å¼æ°ã¯2ã¤ã»çªå·ã¨ãã¬ã¼ã ãªãã¸ã§ã¯ã """ print 'func(): %d, %s' % (num, str(frame)) # ããã§ã¯SIGINTã®æ°å¤ãnumã«å ¥ã
ãã¹ã
ä¸ã®ã³ã¼ãã端æ«ä¸ã§å®è¡ãCtrl+Cãæ¼ãã¨
func(): 2, <frame object at 0x122dbc0>
ã®ãããªåºåããã¦çµäºããã
#! /usr/bin/python # -*- encoding: utf-8 -*- import signal import sys import os try: import pygtk pygtk.require('2.0') except: pass try: import gtk except: sys.exit(1) class MainWindow(gtk.Window): """ ã¡ã¤ã³ã¦ã£ã³ã㦠""" def __init__(self, *args, **kwargs): gtk.Window.__init__(self, *args, **kwargs) self.set_title('test') self.set_size_request(320, 200) self.__accelgroup = gtk.AccelGroup() self.add_accel_group(self.__accelgroup) self.__item_quit = gtk.ImageMenuItem(gtk.STOCK_QUIT, self.__accelgroup) self.__menu_file = gtk.Menu() self.__menu_file.add(self.__item_quit) self.__item_file = gtk.MenuItem('_File') self.__item_file.set_submenu(self.__menu_file) self.__menubar = gtk.MenuBar() self.__menubar.append(self.__item_file) self.__button = gtk.Button() self.__button.set_label('Test') self.__statusbar = gtk.Statusbar() self.__vbox = gtk.VBox(False, 0) self.__vbox.pack_start(self.__menubar, expand=False, fill=False) self.__vbox.pack_start(self.__button) self.__vbox.pack_start(self.__statusbar, expand=False, fill=False) self.add(self.__vbox) self.__button.connect('clicked', self.__on_button_clicked) self.__item_quit.connect('activate', gtk.main_quit) self.connect('delete_event', gtk.main_quit) def __on_button_clicked(self, widget): """ ãã¿ã³ãæ¼ãããã¨ãã®å¦ç """ print '__on_button_clicked()' class PyGTKUnixSigHandlerTest: """ UNIXã·ã°ãã«ãã³ãã©ã®ãã¹ã """ def main(self): """ ã¦ã£ã³ãã¦ãä½æãã¦GTK+ã®ã¡ã¤ã³ã«ã¼ããå¼ã¶ """ # ã·ã°ãã«SIGINT(Ctrl+Cãªã©ã§çºç)ãåãåã£ãã¨ãã«é¢æ°func()ãå¼ã¶ signal.signal(signal.SIGINT, func) win = MainWindow() win.show_all() gtk.main() def func(num, frame): """ UNIXã·ã°ãã«SIGINTã®ãã³ãã© å¼æ°ã¯2ã¤ã»çªå·ã¨ãã¬ã¼ã ãªãã¸ã§ã¯ã """ print 'func(): %d, %s' % (num, str(frame)) # ããã§ã¯SIGINTã®æ°å¤ãnumã«å ¥ã # ã¡ã¤ã³ã«ã¼ããæãã¦çµäºãããã¨ã«ãã gtk.main_quit() if __name__ == '__main__': app = PyGTKUnixSigHandlerTest() app.main()
(2010/2/9)å¾åã®å½¢ã«åããã¦æ¸ãæ¹ã微調æ´(åä½ã¯åã)
(ãPythonでUNIXシグナルのハンドラを扱う(後半)ãã«ç¶ã)
é¢é£è¨äº:
- シェル上のプロセスの動作状態(フォアグラウンド/バックグラウンド/サスペンド)とその制御について(前半)
- シェル上のプロセスの動作状態(フォアグラウンド/バックグラウンド/サスペンド)とその制御について(後半)
- UNIXシグナルによるプロセス制御について
- PythonでUNIXシグナルのハンドラを扱う(後半)
åèURL: