error while using os.popen() to read command-line output

90 views Asked by At

I wrote a python program script1.py. Its general logic flow is as follows:

while(true)
    if(hasTask)
        print('task flow info print')

Then I wrote another python script called monitor.py, using os.popen() to monitor the console output of script1. py, and after obtaining specific information, sent a message to the Redis channel:

redis_key = "command"
cmd = "python script1.py"
pool = redis.ConnectionPool(host="127.0.0.1")
r = redis.Redis(connection_pool=pool)


with os.popen(cmd,"r") as stream:
    while True:
        buf = stream.readline().strip()
        if re.match("target",buf) is not None:

        message = stream.readline().strip()
        
        command_info["command"] = message
        
        r.publish(redis_key, json.dumps(command_info))

In the beginning, the monitor can correctly read the script output and send messages to Redis. The problem is that after some time, this combination does not seem to work properly, and no messages are sent to Redis. Why does this happen?

Is the file object returned by popen is too large? or how can I deal with it, need your help.

0

There are 0 answers