我希望在我的python程序中实现类似程序的尾部。
我可以通过Unix收听命名管道:
tail -f mypipe
这很好用(我可以将字符串回显到所说的管道),但我想在python中这样做。 我希望将通过管道传输的数据设置为变量(对于var,最好使用通过管道发送的最后一个数据进行更新)。
到目前为止,我有:
cwd = os.getcwd()
testpath = os.path.join(cwd, 'mypipe')
if os.path.exists(testpath):
os.remove(testpath)
mkfifo = os.mkfifo(testpath, 0644)
rp = open(testpath, 'r')
response = rp.read()
print 'response is ', response
有两个问题:
response
读取一次,但我不知道如何在收到数据时读取数据流(类似听众)我尝试了类似于here(最高评价的答案)的内容,但它似乎并不适合我。我收到一个错误:
IOError: [Errno 29] Illegal seek