需求:
当后台新增或修改交易币对时,触发自动获取各个平台的交易对价格
如下图,在flask视图中新增或者修改了交易币对,就调用restart_get_new_price()这个函数即可
def get_process_id(name):
import subprocess
child = subprocess.Popen(["pgrep", "-f", name], stdout=subprocess.PIPE, shell=False)
response = child.communicate()[0]
return response
def restart_get_new_price():
import os
# 获取进程id
pid = get_process_id("python /usr/local/gtrading-project/Gtrading/app/utils/get_new_price.py")
if pid:
print(pid.decode())
list1 = pid.decode().split('\n')
for i in list1:
if i:
# kill -9 杀掉get_new_price.py这个进程
result = os.system("kill -9 " + i)
if result == 0:
print('kill success', i)
# get_new_price.py这个文件没有运行,
time.sleep(2)
print('no target pid')
# nohup是后台运行的意思
b = os.system("nohup python /usr/local/gtrading-project/Gtrading/app/utils/get_new_price.py > out.log 2>&1 &")
print('b is', b)
里面的python不用指定是哪个python解释器,它和flask项目运行的python解释器是同一个