惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
小众软件
小众软件
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
量子位
N
Netflix TechBlog - Medium
B
Blog
P
Proofpoint News Feed
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
AI
AI
Stack Overflow Blog
Stack Overflow Blog
U
Unit 42
M
MIT News - Artificial intelligence
Vercel News
Vercel News
D
DataBreaches.Net
P
Palo Alto Networks Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Hacker News
The Hacker News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
Attack and Defense Labs
Attack and Defense Labs
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
美团技术团队
博客园 - 司徒正美
Cloudbric
Cloudbric
J
Java Code Geeks
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
A
About on SuperTechFans

博客园 - 玛瑙河

用matplotlib绘制带误差的条形图及中英文字体设置 修改gnome-shell扩展“Applications Menu”的菜单区域宽度。 VirtualBox 4.3.18 启动虚拟机时显示不能加载 R3模块并退出故障解决一例 自定义IPython Qt Console 窗口大小、字体、颜色 Windows Server 2012 R2 两个奇葩问题的解决 cherokee +php fastcgi 出现 No input file specified 故障一例 ubuntu下从源码编译安装cherokee liftweb整合ckfinder进行文件上传与管理 命令行下多线程下载工具 Axel 2.4 for Windows 正确设置H2数据库的Collation解决中文排序问题 解决MASM编程对话框中文问题 清除U盘子目录变成1K大小的快捷方式病毒的脚本 Scala 脚本的 pound bang 魔术 Google URL Shorter HTML中常用的特殊字符实体编码(Character entity references in HTML 4) - 玛瑙河 MrBayes v3.2 for windows 并行版下载及 checkpoint 功能介绍 强制垃圾回收解决.NET Office互操作中文件锁未能释放的问题 童言无忌之小瑈与甲虫 MrBayes v3.1.2 for windows 并行版下载
Python中用MacFSEvents模块监视MacOS文件系统改变一例
玛瑙河 · 2016-05-31 · via 博客园 - 玛瑙河

最近一个项目中用gulp-watch不能满足需求,于是想到了用Python来解决问题。在安装了MacFSEvents模块后,写了下面一个小程序。

#!/usr/bin/env python2
#-*- coding: utf-8 -*-

import os,sys,signal                    
from   fsevents import Observer
from   fsevents import Stream  

def callback(FileEvent):
    # attributes of FileEvent:mask, cookie and name.
    # mask: 512-delete;256-create;2-changed
    if FileEvent.name.endswith("index.html") and FileEvent.mask == 256:
        print "site rebuild. redeploy extra assets"
        os.system("gulp")
    elif FileEvent.name.endswith(".scss") and FileEvent.mask == 2:
        print "scss changed! compile and redeploy extra assets"
        os.system("gulp sass")

if __name__ == '__main__':
    observer  = Observer()
    stream    = Stream(callback, ".", file_events=True)
    observer.schedule(stream)
    observer.start()  
    #按Control+\强制结束
 

    但是运行时发现只能用Control+\强杀进程,而不能用Control+C结束。

    经google搜索及自行研究结果,得到下面这段程序。用fork子进程的方法,使得进程响应Control+C 退出。

 1 #!/usr/bin/env python2
 2 #-*- coding: utf-8 -*-
 3 
 4 import os,sys,signal                    
 5 from   fsevents import Observer
 6 from   fsevents import Stream  
 7 
 8 def callback(FileEvent):
 9     # attributes of FileEvent:mask, cookie and name.
10     # mask: 512-delete;256-create;2-changed;...
11     if FileEvent.name.endswith("index.html") and FileEvent.mask == 256:
12         print "site rebuild. redeploy extra assets"
13         os.system("gulp")
14     elif FileEvent.name.endswith(".scss") and FileEvent.mask == 2:
15         print "scss changed! compile and redeploy extra assets"
16         os.system("gulp sass")
17 
18 def child():
19     observer  = Observer()
20     stream    = Stream(callback, ".", file_events=True)
21     observer.schedule(stream)
22     observer.start()
23 
24 class Watcher:  
25     """ 
26      创建一个做苦工的子进程。然后父进程等待
KeyboardInterrupt并杀掉子进程。
27
28 """ 29 30 def __init__(self): 31 self.child = os.fork() 32 if self.child == 0: 33 child() 34 else: 35 self.watch() 36 37 def watch(self): 38 try: 39 os.wait() 40 except KeyboardInterrupt: 41 #捕获 Control+C,杀掉子进程 42 print 'KEYBOARDINTERRUPT\n' 43 self.kill() 44 sys.exit() 45 46 def kill(self): 47 try: 48 os.kill(self.child, signal.SIGKILL) 49 except OSError: pass 50 51 52 if __name__ == '__main__': 53 Watcher()