开发者

Python strange file read behaviour for long-running non-block thread reading

开发者 https://www.devze.com 2023-03-20 07:32 出处:网络
I am writing a code for non-block reading file from standard output using threading. I use the online example to start. The file is created when the subprocess is started. However, after read file in

I am writing a code for non-block reading file from standard output using threading. I use the online example to start. The file is created when the subprocess is started. However, after read file in ResultEvent, the file is cleared. I could not debug into thread and not sure what happend there. Anybody give me a clue? Here is the code:

EVT_RESULT_ID = wx.NewId()
def EVT_RESULT(win, func):
    win.Connect(-1, -1, EVT_RESULT_ID, func)

class ResultEvent(wx.PyEvent):
    def __init__(self, data,file):
        wx.PyEvent.__init__(self)
        self.SetEventType(EVT_RESULT_ID)
        TOTAL = 0
        f = open(file, "r")
        while f:
            line = f.readline()
            s = line.split()
            n = len(s)
            if n == 0:
                break
            try:
                TOTAL += float( s[ n-1 ] )
            except:
                pass
        print TOTAL,line,file

        uu = f.read()
        print "self str start"
        print uu,file,data
        f.close()

class WorkerThread(Thread):
    def __init__(self, notify_window, exe,file):开发者_如何学C
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self._notify_window = notify_window
        self.exe = exe
        self.file = file
        self.start()

    def run(self):
        f = open(self.file, 'w')
        p = subprocess.Popen(exe, shell=False, stdout=subprocess.PIPE)
        f.close()
        self.str = p.stdout.readline()
        print "create file"
        print self.str,self.file

        wx.PostEvent(self._notify_window, ResultEvent(self.str,self.file))

The result is the print after "create file" is correct. There is something writting to the file. The print in ResultEvent is wrong. I got TOTAL as 0 and I also checked the file it is empty.


There are several things wrong with your code:

1) You are calling Popen but not waiting for it to finish - the rest of your code is probably executing before the pipe has finished being created. The next thing you do is close the file you are using for stdout, which is why it is always empty. Call p.wait() to wait for the process to finish before you close f.

2) while f: will loop forever (or until you break out of the loop or throw and exception). Files, like most objects, are always treated as True. This is probably not what you mean to do.

3) the readline() call is unnecessary, since file objects are iterable. It is more pythonic (and safer) to do:

for line in f:
    do stuff with line

4) the code reads lines from the file until you hit an empty line (i.e. len(line.split()) == 0). If your file has a blank line somewhere in the middle you will only get the total for part of the file.

5) you call f.read() after you have already iterated through the file. This will only read from the current position in the file to end, so if you have already read all the lines in the file this will read nothing. If you want to read in the entire file at this point either seek back to the beginning or close and reopen the file.

6) s[n-1] is unnecessary. Use s[-1] to get the last element in a list.


To execute a command as a subprocess you should use this syntax checking also if there have been failures in the command execution:

pipe = subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
stdout, stderr = pipe.communicate()
exitcode = pipe.returncode

Also you do not need to open the file before executing the command and after the command execution (what is that meant for?).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号