/usr/share/pyshared/chemfp/Watcher.py is in python-chemfp 1.1p1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 64 65 66 67 68 69 70 71 | """Helper code for multi-threaded Python programs
Quoting from
http://code.activestate.com/recipes/496735-workaround-for-missed-sigint-in-multithreaded-prog/
Multithreaded Python programs often ignore the SIGINT generated by a
Keyboard Interrupt, especially if the thread that gets the signal is
waiting or sleeping. This module provides a workaround by forking a
child process that executes the rest of the program while the parent
process waits for signals and kills the child process.
How to use:
from chemfp import Watcher
def main():
...
Watcher.Watcher()
... start multi-threaded code ...
if __name__ == "__main__":
main()
Created by Allen Downey and distributed under the PSF license for Python.
"""
import threading, time, os, signal, sys
class Watcher(object):
"""this class solves two problems with multithreaded
programs in Python, (1) a signal might be delivered
to any thread (which is just a malfeature) and (2) if
the thread that gets the signal is waiting, the signal
is ignored (which is a bug).
The watcher is a concurrent process (not thread) that
waits for a signal and the process that contains the
threads. See Appendix A of The Little Book of Semaphores.
http://greenteapress.com/semaphores/
I have only tested this on Linux. I would expect it to
work on the Macintosh and not work on Windows.
"""
def __init__(self):
""" Creates a child thread, which returns. The parent
thread waits for a KeyboardInterrupt and then kills
the child thread.
"""
self.child = os.fork()
if self.child == 0:
return
else:
self.watch()
def watch(self):
try:
os.wait()
except KeyboardInterrupt:
# I put the capital B in KeyBoardInterrupt so I can
# tell when the Watcher gets the SIGINT
print 'KeyBoardInterrupt'
self.kill()
sys.exit()
def kill(self):
try:
os.kill(self.child, signal.SIGKILL)
except OSError: pass
|