Module errgrep.non_blocking_read_thread

Expand source code
import queue
import sys
import threading
import time

class NonBlockingReadThread(threading.Thread):
    '''
    A thread that continually reads lines from a file object and places each read line in
        .lines_queue

    This runs as a daemon thread and should be considered a singleton for a given file_like_obj.
    '''
    lines_queue: queue.Queue = queue.Queue()
    def __init__(self, file_like_obj):
        self.file_like_obj = file_like_obj
        threading.Thread.__init__(self, daemon=True)

    def run(self):
        for line in self.file_like_obj:
            line = line.rstrip('\r\n')
            self.lines_queue.put_nowait(line)

        while not self.lines_queue.empty():
            time.sleep(.01)

class StdinReadThread(NonBlockingReadThread):
    ''' A NonBlockingReadThread for stdin '''
    def __init__(self):
        NonBlockingReadThread.__init__(self, sys.stdin)

    def start_if_not_started_yet(self):
        try:
            NonBlockingReadThread.start(self)
        except RuntimeError:
            pass

stdin_read_thread = StdinReadThread()

Classes

class NonBlockingReadThread (file_like_obj)

A thread that continually reads lines from a file object and places each read line in .lines_queue

This runs as a daemon thread and should be considered a singleton for a given file_like_obj.

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.init()) before doing anything else to the thread.

Expand source code
class NonBlockingReadThread(threading.Thread):
    '''
    A thread that continually reads lines from a file object and places each read line in
        .lines_queue

    This runs as a daemon thread and should be considered a singleton for a given file_like_obj.
    '''
    lines_queue: queue.Queue = queue.Queue()
    def __init__(self, file_like_obj):
        self.file_like_obj = file_like_obj
        threading.Thread.__init__(self, daemon=True)

    def run(self):
        for line in self.file_like_obj:
            line = line.rstrip('\r\n')
            self.lines_queue.put_nowait(line)

        while not self.lines_queue.empty():
            time.sleep(.01)

Ancestors

  • threading.Thread

Subclasses

Class variables

var lines_queue : queue.Queue

Methods

def run(self)

Method representing the thread's activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

Expand source code
def run(self):
    for line in self.file_like_obj:
        line = line.rstrip('\r\n')
        self.lines_queue.put_nowait(line)

    while not self.lines_queue.empty():
        time.sleep(.01)
class StdinReadThread

A NonBlockingReadThread for stdin

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.init()) before doing anything else to the thread.

Expand source code
class StdinReadThread(NonBlockingReadThread):
    ''' A NonBlockingReadThread for stdin '''
    def __init__(self):
        NonBlockingReadThread.__init__(self, sys.stdin)

    def start_if_not_started_yet(self):
        try:
            NonBlockingReadThread.start(self)
        except RuntimeError:
            pass

Ancestors

Class variables

var lines_queue : queue.Queue

Methods

def start_if_not_started_yet(self)
Expand source code
def start_if_not_started_yet(self):
    try:
        NonBlockingReadThread.start(self)
    except RuntimeError:
        pass

Inherited members