相当于unix' tail'在python中

时间:2015-08-22 22:04:02

标签: python bash unix

我希望在我的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

有两个问题:

  1. response读取一次,但我不知道如何在收到数据时读取数据流(类似听众)
  2. 我似乎遇到了阻塞问题,管道被声明并且不允许其他程序访问它。我不完全理解在没有块的情况下访问fifo管道,并且我很难获得文档。
  3. 我尝试了类似于here(最高评价的答案)的内容,但它似乎并不适合我。我收到一个错误:

    IOError: [Errno 29] Illegal seek

0 个答案:

没有答案