<commit_before>import os
import signal
import subprocess
import sys
import time
import unittest

from aiy.vision.inference import InferenceEngine

from .test_util import test_image_path

ENV = {
    'PYTHONUNBUFFERED': '1',
    'PYTHONPATH': os.path.join(os.path.dirname(__file__), '..'),
}

def model_path(name):
    return os.path.join('/home/pi/models', name)

def example_path(name):
    p = os.path.join(os.path.dirname(__file__), '..', 'examples', 'vision', name)
    return os.path.abspath(p)

def wait_terminated(process, timeout):
    start = time.monotonic()
    while time.monotonic() - start < timeout:
        code = process.poll()
        if code is not None:
            return code
    return None

class VisionExamplesTest(unittest.TestCase):
    def setUp(self):
        with InferenceEngine() as engine:
            engine.reset()

    def execute(self, args, timeout):
        file, *rest = args
        cmd = [sys.executable, example_path(file)] + rest
        print(cmd)
        process = subprocess.Popen(cmd, shell=False, env=ENV)
        code = wait_terminated(process, timeout)
        if code is not None:
            self.assertEqual(0, code)
            return

        print('Interrupting process (Control-C)')
        os.kill(process.pid, signal.SIGINT)
        if wait_terminated(process, timeout=5.0) is None:<commit_msg>Add poll interval to avoid high CPU usage.<commit_after>import os
import signal
import subprocess
import sys
import time
import unittest

from aiy.vision.inference import InferenceEngine

from .test_util import test_image_path

ENV = {
    'PYTHONUNBUFFERED': '1',
    'PYTHONPATH': os.path.join(os.path.dirname(__file__), '..'),
}

def model_path(name):
    return os.path.join('/home/pi/models', name)

def example_path(name):
    p = os.path.join(os.path.dirname(__file__), '..', 'examples', 'vision', name)
    return os.path.abspath(p)

def wait_terminated(process, timeout, poll_interval=1.0):
    start = time.monotonic()
    while time.monotonic() - start < timeout:
        code = process.poll()
        if code is not None:
            return code
        time.sleep(poll_interval)
    return None

class VisionExamplesTest(unittest.TestCase):
    def setUp(self):
        with InferenceEngine() as engine:
            engine.reset()

    def execute(self, args, timeout):
        file, *rest = args
        cmd = [sys.executable, example_path(file)] + rest
        print(cmd)
        process = subprocess.Popen(cmd, shell=False, env=ENV)
        code = wait_terminated(process, timeout)
        if code is not None:
            self.assertEqual(0, code)
            return

        print('Interrupting process (Control-C)')
        os.kill(process.pid, signal.SIGINT)
        if wait_terminated(process, timeout=5.0) is None:
{'fraction_non_alphanumeric': 0.11648745519713262, 'fraction_numerical': 0.0035842293906810036, 'mean_word_length': 2.892608089260809, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5505343', 'n_tokens_mistral': 872, 'n_tokens_neox': 863, 'n_words': 218}