监控另一个进程(svn)
我有一个Python脚本,用来从一些代码库下载源代码,其中有些代码库比较大。
有时候,svn在下载的过程中会卡住。有没有办法监控一下svn的运行状态,这样我就能知道它是不是卡住了?
2 个回答
0
你可以不断检查svn程序的输出,看看多久会有新文件出现。如果在x秒内没有新文件出现,就重启这个程序。
在你的主脚本中使用子进程来启动svn,同时在等待程序完成的时候检查它的输出。
1
你可以使用 PySVN,并为每个“事件”注册一个回调函数。PySVN 还可以轮询一个“取消”回调函数。第一个回调函数可以启动一个计时器,如果计时器到时间了,你可以告诉“取消”回调函数返回 False,这样就可以取消检出操作。
#!/usr/bin/python
url = "svn://server/path/to/repo"
path = "/path/to/local/wc"
import pysvn
import threading
# Set to something reasonable
SVN_TIMEOUT = 1000
svn_timer = None
stop_svn = False
def timer_expired():
# Too long since last SVN event, so do something sensible...
print "SVN took too long!"
global stop_svn
stop_svn = True
def svn_cancel():
return stop_svn
def notify( event_dict ):
global svn_timer
if svn_timer:
svn_timer.cancel()
svn_timer = threading.Timer(SVN_TIMEOUT, timer_expired)
svn_timer.start()
svn_client = pysvn.Client()
svn_client.callback_notify = notify
svn_client.callback_cancel = svn_cancel
svn_timer = threading.Timer(SVN_TIMEOUT, timer_expired)
svn_timer.start()
revision = svn_client.checkout(url,path)
if svn_timer:
svn_timer.cancel()