同时读取和写入文件 [英] Reading and Writing to a file simultaneously
问题描述
window = Tk()
canvas = Canvas(window,width = WIDTH,height = HEIGHT,bg =#000000)
canvas.pack()
img = PhotoImage =(WIDTH / 2,HEIGHT / 2),image = img,state =normal)
def redraw():
fp = open('test_data.txt','r')
lines = fp.readlines()
在范围内(len(lines)):
#do stuff $ (35,重画)
$ b $ window.after(35,重画)
mainloop()
fp.close()
window.after c $ c>
这就是阅读。
$ b
从多个进程中读取和写入文件可能是不可预知的。如果你运行的是类Unix系统,你可以使用 mkfifo
来创建一个文件您可以同时写入和读取的对象,数据将保持正确的顺序。
在Windows上,您需要一个NamedPipe - 您可以从Python创建然后通过打开SystemVerilog中的普通文件来连接(我相信!)
http://docs.activestate.com/activepython/2.4/pywin32/win32pipe.html
I have a module in written in System Verilog that dumps the contents of the SRAM into a file. I would like to read from this file and use the data in a separate program written in python, but in real time. I don't have much control over the writing from the verilog code. Is it possible to somehow manage the two read and writes? Currently when it reads from the file, there is a (seemingly) random number inserted at the start of every line and that throws off the parsing. I assume these prefixes only appear when they are reading and writing at the same time because if I run them both very slowly it works fine.
window = Tk()
canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="#000000")
canvas.pack()
img = PhotoImage(width=WIDTH, height=HEIGHT)
canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal")
def redraw():
fp = open('test_data.txt','r')
lines=fp.readlines()
for i in range(len(lines)):
#do stuff
fp.close()
window.after(35,redraw)
window.after(35,redraw)
mainloop()
This is what is reading.
Any suggestions are appreciated.
Reading and writing a file from multiple processes is likely to be unpredictable.
If you are running on a Unix-like system, you could use mkfifo
to make a file-like object which you can write to and read from simultaneously and the data will stay in the correct order.
On Windows you need a NamedPipe - which you could create from Python and then connect to by opening as a normal file in SystemVerilog (I believe!)
http://docs.activestate.com/activepython/2.4/pywin32/win32pipe.html
这篇关于同时读取和写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!