且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

只有如果没有运行,用cron运行python脚本

更新时间:1970-01-01 07:55:06

唯一的建议是让你的异常处理一点更具体。您不想不小心删除 fcntl 导入一天,并隐藏结果 NameError 。总是尝试捕获你想要处理的最具体的异常。在这种情况下,我建议如下:

The only suggestion I would make is to make your exception handling a little more specific. You don't want to accidentally delete the fcntl import one day and hide the NameError that results. Always try to catch the most specific exception you want to handle. In this case, I suggest something like:

import errno

try:
    fcntl.lock(...)
except IOError, e:
    if e.errno == errno.EAGAIN:
        sys.stderr.write(...)
        sys.exit(-1)
    raise

这样,任何 >无法获取锁的原因显示(可能在您使用cron时的电子邮件中),您可以决定是否需要管理员查看,程序要处理的另一种情况或其他情况。

This way, any other cause of the lock being unobtainable shows up (probably in your email since you're using cron) and you can decide if it's something for an administrator to look at, another case for the program to handle, or something else.