Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

543
Visualizações
Read file with timeout in Python

Within Linux, there is a file, /sys/kernel/debug/tracing/trace_pipe, which as the name says, is a pipe. So, let's say I want to read the first 50 bytes from it using Python - and I run the following code:

$sudo python -c 'f=open("/sys/kernel/debug/tracing/trace_pipe","r"); print f; print f.read(50); f.close()<br>
<open file '/sys/kernel/debug/tracing/trace_pipe', mode 'r' at 0xb7757e90>

We can see that opening the file goes fast ( if we have the superuser permissions ) - however, if the trace_pipe file is empty at that moment, it will simply block ( and even if there is content, the content will be dumped until there is no more, and then again the file will block ). Then I have to press Ctrl-C to interrupt the Python script with a KeyboardInterrupt...

How can I have Python 2.7 do a read with timeout?

That is, I want to instruct Python to "try read 50 bytes from this file; if you don't succeed after one second, give up and return"?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use

os.read(f.fileno(), 50)

instead. That does not wait until the specified amount of bytes has been read but returns when it has read anything (at most the specified amount of bytes).

This does not solve your issue in case you've got nothing to read from that pipe. In that case you should use select from the module select to test whether there is something to read.

EDIT:

Testing for empty input with select:

import select
r, w, e = select.select([ f ], [], [], 0)
if f in r:
  print os.read(f.fileno(), 50)
else:
  print "nothing available!"  # or just ignore that case
over 4 years ago · Santiago Trujillo Relatório

0

f = os.open("/sys/kernel/debug/tracing/trace_pipe", os.O_RDONLY|os.O_NONBLOCK)

Should prevent blocking (works in Unix only).. No need for select here..

over 4 years ago · Santiago Trujillo Relatório

0

Just adding this as note, for better formatting:

@Alfe's answer in my case:

$ sudo python -c 'import os, select; 
f=open("/sys/kernel/debug/tracing/trace_pipe","r"); print f; 
rrdy, wrdy, xrdy = select.select([f], [], [], 1); print rrdy, wrdy, xrdy ; 
timeout= "timed out" if (rrdy==[]) else "" ; 
print timeout; 
print os.read(f.fileno(), 50) if timeout=="" else ""; 
f.close() '

If there is something in the file, I get response like:

<open file '/sys/kernel/debug/tracing/trace_pipe', mode 'r' at 0xb76f0e90>
[<open file '/sys/kernel/debug/tracing/trace_pipe', mode 'r' at 0xb76f0e90>] [] []

            Xorg-1033  [001] 12570.075859: <user s

If there is nothing in the file, I get:

<open file '/sys/kernel/debug/tracing/trace_pipe', mode 'r' at 0xb7831e90>
[] [] []
timed out

Note that the select documentation isn't explicit that the timeout parameter is in seconds - but that floating point values (e.g. 0.5) also work.

@GabiMe's answer:

$ sudo python -c 'import os; 
filno = os.open("/sys/kernel/debug/tracing/trace_pipe", os.O_RDONLY|os.O_NONBLOCK); 
f=os.fdopen(filno, "r"); print f; 
print "A", f.read(50); 
print "B", os.read(f.fileno(), 50); 
f.close() '

If there is something in the file, I get response like:

<open file '<fdopen>', mode 'r' at 0xb77b6e90>
A             bash-13777 [000] 13694.404519: sys_exi
B            Timer-31065 [001] 13694.404830: sys_exi

If there is nothing in the file, I get:

<open file '<fdopen>', mode 'r' at 0xb77c1e90>
A
Traceback (most recent call last):
  File "<string>", line 1, in <module>
IOError: [Errno 11] Resource temporarily unavailable

... so one must run this in a try block, to catch the IOError, if there is nothing in the file... (both os.read and f.read will raise this exception)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda