python运行过程中改代码_正在运行的Python进程,修改进程使用的变量

一个Python脚本,想要留个接口,在进程运行过程中,修改一个使用的变量,该怎么弄呢~~~

一个笨办法:使用文件通信!!!

一个简单例子:获取某个进程的pid进行处理,指定一个pid进行处理

实现:指定的pid,通过解析文件获取,文件存在,则处理文件内容,不存在,则处理原有内容。

#!/usr/bin/env python

import os

import commands

import time

def main():

num = 0

while True:

pid = 0

if os.path.exists("pid.log"):

with open("pid.log") as fp:

pid = fp.read().strip("\r\n")

if not pid:

cmd = "ps ux | grep -v grep | grep lsd | awk {'print $2'}"

status, output = commands.getstatusoutput(cmd)

pid = output.strip()

print "pid is %s" % pid

num += 1

time.sleep(5)

if num > 100:

break

if __name__ =&#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值