Added threading functions
This commit is contained in:
parent
27d348bf9c
commit
0636a032be
1 changed files with 44 additions and 1 deletions
|
|
@ -1,10 +1,13 @@
|
|||
"""WizardKit: Executable functions"""
|
||||
"""WizardKit: Execution functions"""
|
||||
#vim: sts=2 sw=2 ts=2
|
||||
|
||||
import logging
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from threading import Thread
|
||||
from queue import Queue, Empty
|
||||
|
||||
import psutil
|
||||
|
||||
|
||||
|
|
@ -12,6 +15,38 @@ import psutil
|
|||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Classes
|
||||
class NonBlockingStreamReader():
|
||||
"""Class to allow non-blocking reads from a stream."""
|
||||
# pylint: disable=too-few-public-methods
|
||||
# Credits:
|
||||
## https://gist.github.com/EyalAr/7915597
|
||||
## https://stackoverflow.com/a/4896288
|
||||
|
||||
def __init__(self, stream):
|
||||
self.stream = stream
|
||||
self.queue = Queue()
|
||||
|
||||
def populate_queue(stream, queue):
|
||||
"""Collect lines from stream and put them in queue."""
|
||||
while True:
|
||||
line = stream.read(1)
|
||||
if line:
|
||||
queue.put(line)
|
||||
|
||||
self.thread = start_thread(
|
||||
populate_queue,
|
||||
args=(self.stream, self.queue),
|
||||
)
|
||||
|
||||
def read(self, timeout=None):
|
||||
"""Read from queue if possible, returns item from queue."""
|
||||
try:
|
||||
return self.queue.get(block=timeout is not None, timeout=timeout)
|
||||
except Empty:
|
||||
return None
|
||||
|
||||
|
||||
# Functions
|
||||
def build_cmd_kwargs(cmd, minimized=False, pipe=True, shell=False, **kwargs):
|
||||
"""Build kwargs for use by subprocess functions, returns dict.
|
||||
|
|
@ -132,6 +167,14 @@ def run_program(cmd, check=True, pipe=True, shell=False, **kwargs):
|
|||
return subprocess.run(**cmd_kwargs)
|
||||
|
||||
|
||||
def start_thread(function, args=None, daemon=True):
|
||||
"""Run function as thread in background, returns Thread object."""
|
||||
args = args if args else []
|
||||
thread = Thread(target=function, args=args, daemon=daemon)
|
||||
thread.start()
|
||||
return thread
|
||||
|
||||
|
||||
def wait_for_procs(name, exact=True, timeout=None):
|
||||
"""Wait for all process matching name."""
|
||||
LOG.debug('name: %s, exact: %s, timeout: %s', name, exact, timeout)
|
||||
|
|
|
|||
Loading…
Reference in a new issue