python - will using the command os.lstat("some_file. txt").st_size block the file so other programs cannot write? -
i having hard time finding way asynchronously read/write text file due python blocking file when opens it.
i need read file programme writes it. 1 time other programme writes txt file python read it, key data, , erase text file ready next batch of info (the info comes in batches because test reports ram tester).
so i'm wondering if os.lstat("some_file. txt").st_size
command block file while collecting size of file. if doesn't block run command in loop , utilize size of file trigger python open, read, delete , close text file since info written text file 404 characters @ time every 3-52 seconds.
i'm trying avoid situation python has text file open read/delete while other programme goes write it.
i using windows platform.
you need post-process 3rd party programme writes in text file. best solution if possible pipe output of first programme yours. in case, process standard input.
if other programme writes true file, should read it, , never ever seek write there. simply, @ eof, must still wait until more info available or first programme terminates.
you cannot maintain file open when reach eof note position close , reopen it. way can see whether new lines added file. add together short sleep because active loop , want kind processor , global timeout exit programme if nil else happens long time.
here possible implementation :
import re import time filename = 'yup.txt' # file scan sleep = 0.5 # time sleep (0,5 second) after end of file detection timeout = 240 # total delay in sleep unit before timeout (240 * 0.5 : 2 minutes) rx = re.compile("pass") pos = 0 while true: open(filename) fd: fd.seek(pos) line in fd: tim = timeout # optional processing line if (rx.search(line)): # line contains pass (anywhere in line) print "*** found pass ***" # processing pass line pos = fd.tell() time.sleep(sleep) tim -= 1 if tim == 0: print '*** timeout ***' break
a possible improvement start ram tester in script , test end of process :
import subprocess prog = subprocess.popen("command line start ram tester", shell=true)
because can test if programme has exited (before or in replacement of timeout test) :
if prog.poll() not none: print " programme exited" break
python asynchronous nonblocking
No comments:
Post a Comment