예제 #1
0
async def await_ffmpeg(function: Callable) -> None:
    """
    Make ffmpeg awaitable
    """
    ffmpeg_coroutine = FFmpegCoroutineFactory.create()

    with ProcessTaskPoolExecutor(max_workers=3, cancel_tasks_when_shutdown=True) as executor:
        await executor.create_process_task(ffmpeg_coroutine.execute, function)
예제 #2
0
async def example_use_case(
        queue_logger: Optional["queue.Queue[LogRecord]"] = None,
        configurer: Optional[Callable[[], Any]] = None) -> List[str]:
    """The example use case of ProcessTaskPoolExecutor for E2E testing."""
    with ProcessTaskPoolExecutor(max_workers=3,
                                 cancel_tasks_when_shutdown=True,
                                 queue=queue_logger,
                                 configurer=configurer) as executor:
        futures = await create_example_process_tasks(executor)
        return await asyncio.gather(*futures)
예제 #3
0
async def main() -> None:
    ffmpeg_coroutine = FFmpegCoroutineFactory.create()

    with ProcessTaskPoolExecutor(max_workers=3,
                                 cancel_tasks_when_shutdown=True) as executor:
        awaitables = (executor.create_process_task(ffmpeg_coroutine.execute,
                                                   create_stream_spec)
                      for create_stream_spec in
                      [create_stream_spec_copy, create_stream_spec_filter])
        await asyncio.gather(*awaitables)
예제 #4
0
def example_use_case_method(
    queue_logger: Optional["queue.Queue[LogRecord]"] = None
) -> Generator[str, None, None]:
    with ProcessTaskPoolExecutor(max_workers=3,
                                 cancel_tasks_when_shutdown=True,
                                 queue=queue_logger) as executor:
        futures = [
            executor.create_process_task(process_cpu_bound, x)
            for x in range(10)
        ]
        FutureWaiter.wait(futures)
        return (future.result() for future in futures)
예제 #5
0
async def create_example_process_tasks(
        executor: ProcessTaskPoolExecutor) -> List["Future[str]"]:
    """Creates example process tasks."""
    futures = []
    for task_id in range(10):
        futures.append(executor.create_process_task(process_cpu_bound,
                                                    task_id))
        # In case of new window in Windows, this method seems to occupy CPU resource in case when without sleep.
        # Otherwise, this method never releases CPU resource until all of CPU bound process finish.
        print("Sleep")
        await asyncio.sleep(0.001)
    return futures
예제 #6
0
async def example_use_case(path_file_input: Path,
                           path_file_output: Path) -> None:
    """The example use case of FFmpegCroutine for E2E testing in case of interrupt."""
    with ProcessTaskPoolExecutor(max_workers=1,
                                 cancel_tasks_when_shutdown=True) as executor:
        task = executor.create_process_task(
            FFmpegCoroutineFactory.create().execute,
            CreateStreamSpecCoroutineFilter(path_file_input,
                                            path_file_output).create,
        )
        LocalSocket.send("Ready")
        await task
예제 #7
0
 async def test_future() -> None:
     """
     Can't test in case process.kill() since it sends signal.SIGKILL and Python can't trap it.
     Function process.kill() stops pytest process.
     see:
       - https://psutil.readthedocs.io/en/latest/#psutil.Process.terminate
       - https://psutil.readthedocs.io/en/latest/#psutil.Process.kill
       - https://docs.python.org/ja/3/library/signal.html#signal.SIGKILL
     """
     with ProcessTaskPoolExecutor(cancel_tasks_when_shutdown=True) as executor:
         future = executor.create_process_task(process_cpu_bound, 1, True)
         pid = LocalSocket.receive()
         process = psutil.Process(int(pid))
         process.terminate()
         with pytest.raises(Terminated):
             await future
         assert future.done()
         assert isinstance(future.exception(), Terminated)
예제 #8
0
async def example_use_case_interrupt(
    path_file_input: str,
    path_file_output: str,
    queue_log_record: queue.Queue[LogRecord],
    configurer: Callable[..., Any],
) -> None:
    """The example use case of FFmpegCroutine for E2E testing in case of interrupt."""
    logger = getLogger(__name__)
    logger.info("Example use case interrupt start")
    with ProcessTaskPoolExecutor(max_workers=3,
                                 cancel_tasks_when_shutdown=True,
                                 queue=queue_log_record,
                                 configurer=configurer) as executor:
        ffmpeg_coroutine = FFmpegCoroutineFactory.create()
        coroutine_create_stream_spec_filter = CreateStreamSpecCoroutineFilter(
            path_file_input, path_file_output)
        future = executor.create_process_task(
            ffmpeg_coroutine.execute,
            coroutine_create_stream_spec_filter.create)
        await future
        raise Exception("Failed")
예제 #9
0
 def test_shutdown() -> None:
     with ProcessTaskPoolExecutor() as executor:
         executor.shutdown()