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)
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)
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)
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)
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
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
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)
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")
def test_shutdown() -> None: with ProcessTaskPoolExecutor() as executor: executor.shutdown()