import time
import signal


def wait_for_process(process, timeout, kill=False) -> int:
    """wait_for_process
    Waits for the input process to finish for the input timeout period. If
    the timeout period expires and the user has supplied the kill flag, a
    kill signal will be sent to the input process

    :param process: process to monitor
    :param timeout: timeout period to wait before returning
    :param kill: boolean indicating if the process should be killed if the
    timeout period expires
    :returns: process exit code
    """
    t_initial = time.time()
    while time.time() - t_initial < timeout:
        if process.poll() == 0:
            return process.poll()
        time.sleep(0.1)
    if kill:
        process.send_signal(signal.SIGKILL)
    return process.poll()