无法立即在python中读取文件的输出

时间:2019-07-31 16:30:49

标签: python python-3.x file io

我正在尝试在python3中为pdb创建调试器包装,该包装将接收pdb定制命令作为输入并提供从pdb接收的输出。在代码中,pdb打开一个文件作为输入,然后开始接收命令。由于阻塞输出流的问题,我将输出写入文件,我已经将两个文件流创建到文件“ tmp_out_file.txt”,一个用于写入,另一个用于读取文件。代码的写入部分正常工作,并且输出已写入文件。但是,当我从文件中读取文件时,stdout上没有显示输出

我已经通过vscode调试器运行了该程序。当我逐行运行程序时,代码工作正常,并将输出打印到stdout。但是,当我连续运行代码或跳过debugger.py中的o = c.reciever('s')行时(调试时),它不会输出任何输出。

Communicator.py

import os
import sys
from subprocess import PIPE, Popen
from threading import Thread 
from watcher import Watcher 

class Communicator:

    def __init__(self, filename):
        self.cur_dir = os.getcwd()
        self.cmd = " ".join(['python', '-m', 'pdb', filename])
        self.tmp_write_stream = open('tmp_out_file.txt', 'wb')
        self.tmp_read_stream = open('tmp_out_file.txt', 'rb')
        self.p = Popen(self.cmd, stdin=PIPE, stdout=self.tmp_write_stream, stderr=self.tmp_write_stream)

    def parse(self, res):
        u = res.split('(Pdb)')
        r = []
        for v in u:
            tmp = v.strip()
            if len(tmp) > 0:
                r.append(tmp)
        return r

    def read_output(self):
        print(self.tmp_read_stream.tell())
        try:
            p = self.tmp_read_stream.read()
            # self.tmp_read_stream.flush()
            p = self.parse(p.decode('utf-8'))
            print(self.tmp_read_stream.tell())
            return p
        except Exception as e:
            print(f"[!!Error]: {e}", flush=True)

    def write_input(self, value):
        print(self.tmp_write_stream.tell())
        value = value.strip('\r')
        value = value.strip('\n')
        value = value + "\r\n"
        print(f"[In]: {value}", flush=True)
        self.p.stdin.write(value.encode('utf-8'))
        self.p.stdin.flush()
        print(self.tmp_write_stream.tell())

    def reciever(self, cmd):
        self.write_input(cmd)
        d = self.read_output()
        try:
            with open('tmp.txt', 'wb') as t:
                t.write(d)
        except Exception as e:
            pass
        return d

    def clear(self):
        return self.read_output()

    def close(self):
        if self.tmp_read_stream != None:
            self.tmp_read_stream.close()
        if self.tmp_write_stream != None:
            self.tmp_write_stream.close()

debugger.py

import communicator as comm
import os


c = comm.Communicator('./test/cmp.py')
# w = wtc.Watcher(c)
# w.push('l')
# w.push('r')
# w.push('arr')
c.clear() 
for i in range(100):
    o = c.reciever('s')
    print(f"[Out]: {o}", flush=True)
    # w.probe()  
c.close()

Watcher.py(调试器监视,以跟踪调试时要监视的变量值)

class Watcher:

    def __init__(self, communicator):
        self.watchVariables = {}
        self.index = 0
        self.keylist = []
        self.communicator = communicator
        self._freeze = False

    def push(self, key):
        if self._freeze == False:
            if key not in self.watchVariables:
                self.watchVariables[key] = None

    def pop(self, key):
        if self._freeze == False:
            if key in self.watchVariables:
                del self.watchVariables[key]

    def probe(self):
        self._freeze = True
        for key in self.watchVariables.keys():
            res = self.communicator.reciever('p ' + key)
            self.watchVariables[key] = res
        self._freeze = False

    def update(self, key, value):
        if key in self.watchVariables:
            self.watchVariables[key] = value

cmp.py(运行调试器的文件)

def partition(arr, l, r):
    i = l - 1
    p = arr[r]
    for j in range(l, r):
        if arr[j] < p:
            i = i + 1
            arr[i], arr[j] = arr[j], arr[i]

    i = i + 1
    arr[i], arr[r] = arr[r], arr[i]
    return (i, arr)

def quicksort(arr, l, r):
    if l < r:
        p, arr = partition(arr, l, r)
        arr = quicksort(arr, l, p - 1)
        arr = quicksort(arr, p + 1, r)
    return arr

ats = [2, 1, 4, 5, 10, 8, 6]
print(quicksort(ats, 0, len(ats) - 1)) 

这是我从vscode调试器运行时收到的正确输出的示例:

0 80 80 161 80 161 出:['> d:\ learning \ deb \ test \ cmp.py(13)()\ r \ n-> def quicksort(arr,l,r):']

这是我从Powershell或终端运行时收到的输出

0 0 0 0 0 0 0 在:p l

0 0 0 0 在:p r

0 0 0 0 在:p arr

0 0 0 0 它甚至没有显示任何一致的输出

0 个答案:

没有答案