[新功能] 服务进程ID运行监控

This commit is contained in:
msincenselee 2019-12-01 07:54:40 +08:00
parent 2e8513fea6
commit 44ebd400cd

77
vnpy/trader/util_pid.py Normal file
View File

@ -0,0 +1,77 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
# 保存进程ID => logs/pid.txt
# 运行开始时检查是否存在之前得pid进程
# 一般使用与运行服务,且唯一进程
# 日志文件路径
logs_path = os.path.abspath(os.path.join(os.getcwd(), 'logs'))
if not os.path.isdir(logs_path):
os.mkdir(logs_path)
assert os.path.isdir(logs_path)
# 记录pid得文件
pid_file = os.path.abspath(os.path.join(logs_path, 'pid.txt'))
def _check_pid(pid):
"""
检查pid是否与当前进程pid一致
:param pid:
:return:
"""
cur_pid = os.getpid()
if cur_pid != pid:
return False
return True
def _check_status():
"""
检查状态
:return:
"""
if os.path.exists(pid_file):
with open(pid_file, 'r') as f:
pid = f.read().strip()
# print(pid)
if pid != "":
if _check_pid(pid):
return pid
return None
def _save_pid(log=True):
"""
保存pid
:param log:
:return:
"""
pid = os.getpid()
if log:
print('pid={}'.format(pid))
with open(pid_file, 'w') as f:
f.write(str(pid))
if log:
print(u'wrote pid file:{}'.format(pid_file))
def update_pid():
"""
更新pid
:return:
"""
_save_pid(log=False)
# 执行检查
if _check_status():
import sys
print(u'another service is already running...', file=sys.stderr)
exit(0)
_save_pid()