取消gpid,改为pid

This commit is contained in:
msincenselee 2018-05-12 14:36:33 +08:00
parent a506d7869a
commit 83dc65544c

View File

@ -4,6 +4,7 @@ import subprocess
import os import os
import sys import sys
import platform import platform
import psutil
run_path = os.path.abspath(os.path.join(os.getcwd(), 'logs')) run_path = os.path.abspath(os.path.join(os.getcwd(), 'logs'))
if not os.path.isdir(run_path): if not os.path.isdir(run_path):
@ -12,6 +13,15 @@ assert os.path.isdir(run_path)
gpid_file = os.path.abspath(os.path.join(run_path, 'gpid.txt')) gpid_file = os.path.abspath(os.path.join(run_path, 'gpid.txt'))
USE_GPID = False
def _check_pid(pid):
cur_pid = os.getpid()
if cur_pid != pid:
return False
return True
def _check_gpid(gpid): def _check_gpid(gpid):
plat = str(platform.system()) plat = str(platform.system())
if plat == 'Windows': if plat == 'Windows':
@ -45,9 +55,13 @@ def _status():
with open(gpid_file, 'r') as f: with open(gpid_file, 'r') as f:
gpid = f.read().strip() gpid = f.read().strip()
# print(gpid) # print(gpid)
if gpid != "" and _check_gpid(gpid): if gpid != "" :
return gpid if USE_GPID:
if _check_gpid(gpid):
return gpid
else:
if _check_pid(gpid):
return gpid
return None return None
if _status(): if _status():
@ -60,11 +74,19 @@ def _save_gpid():
if plat == 'Windows': if plat == 'Windows':
gpid = os.getpid() gpid = os.getpid()
else: # unix else: # unix
gpid = os.getpgrp() gpid = os.getpgrp() if USE_GPID else os.getpid()
print( 'gpid={}'.format(gpid)) print( 'gpid={}'.format(gpid))
with open(gpid_file, 'w') as f: with open(gpid_file, 'w') as f:
f.write(str(gpid)) f.write(str(gpid))
print(u'wrote gpid file:{}'.format(gpid_file)) print(u'wrote gpid file:{}'.format(gpid_file))
_save_gpid() _save_gpid()
if __name__ == '__main__':
test_pid = 20671
print('check pid:{}:result:{}'.format(test_pid, _check_pid(test_pid)))
if psutil.pid_exists(test_pid):
print('pid:{} exist:'.format(test_pid))