我可以使用 Python 异常来处理守护进程的 SIGHUP 信号的后果吗?
我正在用 python 编写一个简单的小型文件同步守护进程,以实现主机器和辅助机器之间的同步文件系统。大部分繁重的工作都是由 rsync 完成的。
在主端,它定期调用 rsync 并在重复之前休眠几秒钟。在辅助端,它使用 subprocess.Popen() 生成 rsyncd 并执行 .wait() 直到退出。但是我想用 SIGHUP 触发守护进程的重新配置。我想知道处理清理的最佳方法是什么。
我最初的想法是让信号处理程序引发一个可以触发清理的异常:
def signal_handler(signum, frame):
raise fsync_config_exception
并且:
rsync_args = [rsync_binary, "--daemon", "--no-detach", "--config=%s" % (config.name) ]
p = subprocess.Popen(rsync_args)
try:
p.wait()
if p.returncode != 0:
print "failed to spawn rsyncd"
return False
except fsync_config_exception:
print "spawn_and_monitor_rsyncd: config exceptions"
except:
(type, value, tb) = sys.exc_info()
print "we got %s with %s instead" % (type, value)
但是我得到一个:
we got <type 'exceptions.TypeError'> with __init__() takes exactly 2 arguments (1 given) instead
而不是预期的 fsync_config_exception。有人对解决此类问题的最佳方法有任何建议吗?我是否通过尝试从信号上下文中引发异常来可怕地滥用异常?
I'm writing a simple little file-syncing daemon in python to implement a synchronised file system between a primary and secondary machine. Most of the heavy lifting is done by rsync.
On the primary side it periodically calls rsync and sleeps for a few seconds before repeating. On the secondary side it spawns rsyncd with subprocess.Popen() and does a .wait() until it exits. However I want to trigger a reconfiguration of the daemon with a SIGHUP. I'm wondering what the best way of handling clean-up is.
My initial thoughts were to have the signal handler raise an exception that can trigger the clean-up:
def signal_handler(signum, frame):
raise fsync_config_exception
And:
rsync_args = [rsync_binary, "--daemon", "--no-detach", "--config=%s" % (config.name) ]
p = subprocess.Popen(rsync_args)
try:
p.wait()
if p.returncode != 0:
print "failed to spawn rsyncd"
return False
except fsync_config_exception:
print "spawn_and_monitor_rsyncd: config exceptions"
except:
(type, value, tb) = sys.exc_info()
print "we got %s with %s instead" % (type, value)
However I get a:
we got <type 'exceptions.TypeError'> with __init__() takes exactly 2 arguments (1 given) instead
instead of the expected fsync_config_exception. Has anyone any suggestions for the best approach to this sort of problem? I'm I hideously abusing Exceptions by trying to raise them from a signal context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该引发一个
fsync_config_exception
实例,而不是类。 (使用其__init__()
签名实例化它)但是,我不建议以这种方式引发异步异常。您将很难保证仅在可以正确处理异常时才引发异常。这也不是一个需要习惯的好模式,因为你不能在 Python 解释器上下文中中断阻塞的 C 扩展调用....(但这对你来说可能不是问题?)
在信号处理程序中,我会(掩盖细节)并且不知道这是否适合您的情况):
You should raise a
fsync_config_exception
instance, not the class. (Instantiate it using its__init__()
signature)However, I do not suggest raising asynchronous exceptions in that manner. You'll have a hard time guaranteeing that the exception is only raised when it can be properly handled. It's also not a good pattern to become accustomed to since you cannot interrupt blocking C extension calls in the Python interpreter context....(but that might not be a problem for you?)
In the signal handler, I would (glossing over details and not knowing if this is correct for your case):