同时读取和写入文件 [英] Reading and Writing to a file simultaneously

查看:125
本文介绍了同时读取和写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个写在System Verilog中的模块,将SRAM的内容转储到一个文件中。我想从这个文件中读取,并使用python中的一个单独的程序中的数据,但实时。我没有太多的控制Verilog代码的写作。有可能以某种方式管理这两个读取和写入?目前,当它从文件中读取时,在每行的开始插入一个(似乎是)随机数,并抛出解析。我假设这些前缀只在他们同时读写的时候出现,因为如果我运行它们的速度都很慢,那么它就可以正常工作了。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆