如何使用 Ctrl-C 退出使用 MacFSEvents 的 Python 程序
我正在使用 MacFSEvents,这是一个 Python 库,用于监视 Mac OS X 上目录的更改,如下所示:
# from http://pypi.python.org/packages/source/M/MacFSEvents/
from fsevents import Observer
from fsevents import Stream
observer = Observer()
def callback(event):
print event.name
stream = Stream(callback, '.', file_events=True)
observer.schedule(stream)
observer.start()
当我在终端中运行此脚本时,按 Ctrl-C 不会退出程序 - 这是我能找到的唯一方法要杀死它,可以在单独的窗口中使用“kill”,或者使用活动监视器等。
关于如何使这样的程序可以通过 Ctrl-C 杀死,有什么想法吗?
I'm using MacFSEvents, a Python library that monitors a directory for changes on Mac OS X, like so:
# from http://pypi.python.org/packages/source/M/MacFSEvents/
from fsevents import Observer
from fsevents import Stream
observer = Observer()
def callback(event):
print event.name
stream = Stream(callback, '.', file_events=True)
observer.schedule(stream)
observer.start()
When I run this script in the Terminal, hitting Ctrl-C doesn't exit the program -- the only way I can find to kill it is with 'kill' in a separate window, or with Activity Monitor, etc.
Any ideas on how to make such a program killable by Ctrl-C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一下,答案是鲜为人知的 Ctrl-\,它向进程发送 SIGQUIT,并且无论挂起的程度如何都会退出它。
The answer, by the way, is the little known Ctrl-\, which sends SIGQUIT to the process, and will quit it no matter how hung.
另一种方法是查找
KeyboardInterrupt
并手动停止观察者(作为observer.run()
的替代方法):An alternative is to look for
KeyboardInterrupt
and stop the observer manually (as an alternative toobserver.run()
):