See All Titles |
![]() ![]() Optional Operational SystemThe next set of modules implements interfaces to optional operational system features. Keep in mind that these features are not available for all platforms. signalThe signal module provides mechanisms to access POSIX signals in order to let the programmer set her own signal handlers for asynchronous events. A good example is the case when it is necessary to monitor the users, checking whether they press CTRL+C to stop the execution of a program. Although Python provides default handlers, you can overwrite them by creating your own. import signal, sys def signal_handler(signal, frame): print "You have pressed CTRL+C" signal.signal(signal.SIGINT, signal.SIG_IGN) print "Now, you can\ 't stop the script with CTRL+C " } "for the next 10 seconds!" signal.signal(signal.SIGALRM, alarm_handler) signal.alarm(10) while 1: print "I am looping" def alarm_handler(signal, frame): print "Now you can leave the program" sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print "Press CTRL+C" while 1: continue Some of the available signals you can use are as follows:
socketThe socket module provides access to a low-level BSD socket-style network interface. See Chapter 10, "Basic Network Background," for details. selectThe select module is used to implement polling and to multiplex processing across multiple I/O streams without using threads or subprocesses. It provides access to the BSD select() function interface, available in most operating systems. On windows it only works for sockets. On UNIX, it is used for pipes, sockets, files, and so on. See Chapter 10 for details. threadThe thread module supports lightweight process threads. It offers a low-level interface for working with multiple threads. See Chapter 9 for details. threadingThe threading module provides high-level threading interfaces on top of the thread module. See Chapter 9 for details. QueueThe Queue module is a synchronized queue class that is used in thread programming to move Python objects between multiple threads. See Chapter 9 for details. anydbmThe anydbm module is a generic dbm-style interface to access variants of the dbm database. See Chapter 8 for details. dumbdbmThe dumbdbm module is a simple, portable, and slow database implemented entirely in Python. See Chapter 8 for details. dbhashThe dbhash module provides a function that offers a dbm-style interface to access the BSD database library. See Chapter 8 for details. whichdbThe whichdb module provides a function that guesses which dbm module (dbm, gdbm, or dbhash) should be used to open a specific database. See Chapter 8 for details. bsddbThe bsddb module provides an interface to access routines from the Berkeley db library. See Chapter 8 for details. zlibThe zlib module provides functions that allow compression and decompression using the zlib library. The compression that is provided by this module is compatible with gzip. For more details check out the zlib library home page at http://www.cdrom.com/pub/infozip/lib. gzipThe gzip module offers support for gzip files. This module provides functions that allow compression and decompression using the GNU compression program gzip. This module has a class named GzipFile that can be used to read and write files compatible with the GNU gzip program. The objects that are generated by this class behave just like file objects. The only exception is that the seek and tell methods aren't part of the standard implementation. >>> import gzip >>> gzipfile = gzip.GzipFile("backup.gz") >>> contents = gzipfile.read() >>> print contents rlcompleterThe rlcompleter module provides a completion function for the readline module. The readline module is a UNIX module that is automatically imported by rlcompleter. It uses a compatible GNU readline library to activate input editing on UNIX.
|
Index terms contained in this sectioncompletion functionfunctions completion libraries Optional Operational System 2nd 3rd modules rlcompleter signal 2nd Optional Operational System library 2nd 3rd rlcompleter module signal module 2nd |
© 2002, O'Reilly & Associates, Inc. |