Read File with Timeout in Python

Read file with timeout in Python

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

Use a timeout to prevent deadlock when opening a file in Python?

You can try using stopit

from stopit import SignalTimeout as Timeout

with Timeout(5.0) as timeout_ctx:
with open('/nfsdrive/foo', 'r') as f:
# do something with f
pass

There may be some issues with SignalTimeout in multithreaded environments (like Django). ThreadingTimeout on the other hand may cause problems with resources on some virtual hostings when you run too many "time-limited" functions

P.S. My example also limits processing time of opened file. To only limit file opening you should use different approach with manual file opening/closing and manual exception handling

Reading a file line-by-line with a timeout for lines that are taking too long?

What you can do is count the number of lines until the problem line and output it - make sure you flush the output - see https://perl.plover.com/FAQs/Buffering.html . Then write another program that will copy the first of this number of lines to a different file, and then read the file's input stream character by character (see http://perldoc.perl.org/functions/read.html ) until it hits a "\n" and then copy the rest of the file - either line by line or in chunks.

python socket file read timeout?

Taken from Python's documentation about socket.makefile():

socket.makefile([mode[, bufsize]])

Return a file object associated with the socket. (File objects are described in File Objects.) The file object references a dup()ped version of the socket file descriptor, so the file object and socket object may be closed or garbage-collected independently. The socket must be in blocking mode (it can not have a timeout). The optional mode and bufsize arguments are interpreted the same way as by the built-in file() function.

Therefore you can't have a timeout on a socket-file, if you need timeouts, you must use regular sockets.

Intentionally cause a read/write timeout?

It seems to me that fsdisk is the right tool your looking for. It can bind your storage and inject errors.



Related Topics



Leave a reply



Submit