# Path: src/dynmen/cmd/blocking.py
# def launch(cmd, fn_input, fn_transform_res=None, **kw):
#     PIPE = _sp.PIPE
#     proc = _sp.Popen(cmd, stdout=PIPE, stdin=PIPE, **kw)
#     bstr = fn_input()
#     stdout, stderr = proc.communicate(bstr)
#     try:
#         proc.terminate()
#     except ProcessLookupError:
#         pass
#     result = ProcStatus(stdout, stderr, proc.returncode)
#     if fn_transform_res is None:
#         return result
#     transformed = fn_transform_res(result)
#     return transformed


from .blocking import launch as _launch
from concurrent.futures import ThreadPoolExecutor
# -*- coding: utf-8 -*-


def launch(cmd, fn_input, fn_transform_res=None, **kw):
    # Do not pass a thread_name_prefix to ThreadPoolExecutor
    # as that is only for py36 and beyond.
    executor = ThreadPoolExecutor(max_workers=1)
    future = executor.submit(_launch, cmd, fn_input, fn_transform_res, **kw)
    future.add_done_callback(lambda x: executor.shutdown(wait=False))

# Path: src/dynmen/cmd/blocking.py
# def launch(cmd, fn_input, fn_transform_res=None, **kw):
#     PIPE = _sp.PIPE
#     proc = _sp.Popen(cmd, stdout=PIPE, stdin=PIPE, **kw)
#     bstr = fn_input()
#     stdout, stderr = proc.communicate(bstr)
#     try:
#         proc.terminate()
#     except ProcessLookupError:
#         pass
#     result = ProcStatus(stdout, stderr, proc.returncode)
#     if fn_transform_res is None:
#         return result
#     transformed = fn_transform_res(result)
#     return transformed


# Path: src/dynmen/cmd/futures.py
from .blocking import launch as _launch
from concurrent.futures import ThreadPoolExecutor

# -*- coding: utf-8 -*-


def launch(cmd, fn_input, fn_transform_res=None, **kw):
    # Do not pass a thread_name_prefix to ThreadPoolExecutor
    # as that is only for py36 and beyond.
    executor = ThreadPoolExecutor(max_workers=1)
    future = executor.submit(_launch, cmd, fn_input, fn_transform_res, **kw)
    future.add_done_callback(lambda x: executor.shutdown(wait=False))

    return future