Python Forum
Python tailing file or named pipe stalls after a while
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python tailing file or named pipe stalls after a while
#1
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?
Reply
#2
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
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é..
Reply
#4
@Polypop77 click on the 'Help / Rules' menu entry at the top of the page. Please post your new thread in english.
Reply
#5
@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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,479 Nov-09-2023, 10:56 AM
Last Post: mg24
  Resolving ImportError: No module named gdb (Python in C++) mandaxyz 3 1,470 Oct-04-2023, 02:43 PM
Last Post: mandaxyz
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,348 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,024 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  BrokenPipeError: [Errno 32] Broken pipe throwaway34 6 9,368 May-06-2021, 05:39 AM
Last Post: throwaway34
  Duplex Named Pipe with Python Server and C# Clients raybowman 1 2,411 Dec-03-2020, 09:58 PM
Last Post: Gribouillis
  2 or more processes on the write end of the same pipe Skaperen 4 3,893 Sep-27-2020, 06:41 PM
Last Post: Skaperen
  STT: recognition connection failed: [Errno 32] Broken pipe GrahamBerends 0 5,093 Jul-18-2020, 11:00 PM
Last Post: GrahamBerends
  multiprocessing Pipe.poll very slow seandepagnier 0 2,364 Mar-09-2020, 03:10 AM
Last Post: seandepagnier
  ImportError: No module named pymysql - On Apache2 for Python 2.7 on Mac Tyrone 9 7,656 Jun-12-2019, 07:08 PM
Last Post: Tyrone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020