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
Google App Engine: Won't Serve Static Assets with Below Error:
Move and Zoom a Tkinter Canvas with Mouse
Plotting the Data with Scrollable X (Time/Horizontal) Axis on Linux
Cannot Get Environment Variables in Django Settings File
How to Lock a Directory Between Python Processes in Linux
Linux Bash Script Running Multiple Python
How to Find Out the Date of the Last Saturday in Linux Shell Script or Python
Can Python Detect Which Os Is It Running Under
Why Can't My Post-Receive Hook Run a Virtualenv Source Command
How to Get an Windows Executable from My Kivy App (Pyinstaller)
How to Simulate Input to Stdin for Pyunit
How to Send a Signal from a Python Program
Auto.Arima() Equivalent for Python
Python Linux Selenium: Chrome Not Reachable
Attribute Bold Doesn't Seem to Work in My Curses
How to Deal with Linux/Python Dependencies
Python Portable, Linux & Windows
Google Cloud Sdk: Set Environment Variable_ Python --> Linux