Python Forum

Full Version: Python tailing file or named pipe stalls after a while
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a GNU radio project outputing data from an FM scanner running on a raspberry pi. I also have a bluetooth service running, which should send any data outputted to stdout by the gnu radio command to any receivers. The bluetooth part is working fine.

To gather data from stdout and send it to the python bluetooth script, I thought i'd use named pipes. I run this in the following manner:

sudo ais_rx --gain 44 --error 5 -s osmocom 2>&1 | sudo tee> /tmp/namedpipe
This should send both stdout en stderr to the pipe in the tmp folder, which was created on the python end like this:

pipe_path = "/tmp/namedpipe"
    if not os.path.exists(pipe_path):
        os.mkfifo(pipe_path)
I then read and print all data in a loop like this:

with open(pipe_path) as fifo:
        print("FIFO opened")
        while True:
            try:               
                data = fifo.readline()
                print(data)
            except Error:
                print ('error')
This works ok for a minute or so, and then just seems to stall. Theres no error, it just stops responding to things written to the pipe.

To rule out any problems with the pipe, I also tried writing to a file and tailing it, like this:

sudo ais_rx --gain 44 --error 5 -s osmocom 2>&1 | sudo tee> /tmp/namedfile
and I then tail it as follows:

f = subprocess.Popen(['tail','-F','/tmp/namedfile'],\
    stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    p = select.poll()
    p.register(f.stdout)

    while True:
        if p.poll(1):
            try:               
                data = f.stdout.readline().strip()            
                print(data)
            except Error:
                print ('error')
        time.sleep(1)
I know the data output doesn't actually stop, because if I tail it from a terminal I can see it coming in. It's just the python side that stops reading it.

Can anyone point me in the right direction?
As far as the first solution goes - file object opened for reading does not follow dynamic changes to the file content. You may store the last read position and reopen file and read from that position (readline will not work), but that will require some partial string management
Bonjour,

Je me permets de poster ce commentaire car je ne comprends pas comment dois-je procéder pour poster un nouveau sujet... pouvez-vous m'aider? merci et désolé..
@Polypop77 click on the 'Help / Rules' menu entry at the top of the page. Please post your new thread in english.
@nilsk123 Try using read()
with open(pipe_path) as fifo:
    print("FIFO opened")
    while True:
        data = fifo.read()
        if not data: # writer process closed the pipe
            break
        print(data)