Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Non-blocking line iterator can return 'None' if stream does not produce data after timeout period
#!/usr/bin/python
import os
import sys
import select
class AsyncLineReader(object):
BUFSIZE = 4096
def __init__(self, stream, timeout=5):
self.stream = stream
self.timeout = timeout
self.lines = []
self.current_fragments = []
self.poll = select.poll()
self.poll.register(
self.stream.fileno(),
select.POLLIN|select.POLLPRI)
def read_or_timeout(self):
if self.poll.poll(self.timeout*1000):
return os.read(self.stream.fileno(), self.BUFSIZE)
return None
def process(self):
buff = self.read_or_timeout()
if buff:
while True:
pos = buff.find(b'\n')
if pos == -1:
self.current_fragments.append(buff)
break
self.current_fragments.append(buff[:pos+1])
buff = buff[pos+1:]
self.lines.append(b''.join(self.current_fragments))
self.current_fragments = []
elif buff == '':
raise StopIteration
def __iter__(self):
return self
def __next__(self):
if not self.lines:
self.process()
if self.lines:
return self.lines.pop(0)
return None
next = __next__
for msg in AsyncLineReader(sys.stdin, timeout=5):
if msg:
print("LINE: ", repr(msg))
else:
print("TIMEOUT...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.