Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python script hangs when writing to /dev/xconsole

my python script is supposed to write to /dev/xconsole. It works as expected, when I am reading from /dev/xconsole, such as with tail -F /dev/xconsole. But if I don't have tail running, my script hangs and waits.

I am opening the file as follows:

xconsole = open('/dev/xconsole', 'w')

and writing to it:

for line in sys.stdin:
    xconsole.write(line)

Why does my script hang, when nobody is reading the output from /dev/xconsole ?

like image 641
Martin Vegter Avatar asked Oct 21 '19 16:10

Martin Vegter


2 Answers

/dev/xconsole is a named pipe and it is on demand FIFO pipe.

So when you use it, it stores data in memory as Linux provides an object of fixed size. If the application doesn't read the data timely, then the buffer becomes full and the application hangs.

In order to avoid this, you'll need to Write > Read > Write and so on. Just ensure that it doesn't fill up. For a Linux system it's around 64KB usually.

like image 193
Vishnudev Avatar answered Oct 24 '22 02:10

Vishnudev


@Vishnudev summarized this nicely already and should be accepted as the correct answer. I'll just add to his answer with the following code to resize your FIFO memory buffer:

import fcntl

F_SETPIPE_SZ = 1031
F_GETPIPE_SZ = 1032

fifo_fd = open("/path/to/fifo", "rb")
print(f"fifo buffer size before: {fcntl.fcntl(fifo_fd, F_GETPIPE_SZ))}"
fcntl.fcntl(fifo_fd, F_SETPIPE_SZ, 1000000)
print(f"fifo buffer size after: {fcntl.fcntl(fifo_fd, F_GETPIPE_SZ))}"
like image 21
smassey Avatar answered Oct 24 '22 04:10

smassey