perf: postprocessing one video only at the same time

This commit is contained in:
acgnhik 2022-06-19 13:32:56 +08:00
parent b74c25ebb6
commit 765df3ead9

View File

@ -4,7 +4,7 @@ import asyncio
import logging import logging
from contextlib import suppress from contextlib import suppress
from pathlib import PurePath from pathlib import PurePath
from typing import Any, Awaitable, Dict, Iterator, List, Optional, Union from typing import Any, Awaitable, Dict, Final, Iterator, List, Optional, Union
from reactivex.scheduler import ThreadPoolScheduler from reactivex.scheduler import ThreadPoolScheduler
@ -48,6 +48,8 @@ class Postprocessor(
AsyncCooperationMixin, AsyncCooperationMixin,
SupportDebugMixin, SupportDebugMixin,
): ):
_worker_semaphore: Final = asyncio.Semaphore(value=1)
def __init__( def __init__(
self, self,
live: Live, live: Live,
@ -127,12 +129,15 @@ class Postprocessor(
@aio_task_with_room_id @aio_task_with_room_id
async def _worker(self) -> None: async def _worker(self) -> None:
while True: while True:
self._status = PostprocessorStatus.WAITING self._status = PostprocessorStatus.WAITING
self._postprocessing_path = None self._postprocessing_path = None
self._postprocessing_progress = None self._postprocessing_progress = None
video_path = await self._queue.get() video_path = await self._queue.get()
async with self._worker_semaphore:
logger.debug(f'Postprocessing... {video_path}') logger.debug(f'Postprocessing... {video_path}')
if not await self._is_vaild_flv_file(video_path): if not await self._is_vaild_flv_file(video_path):
@ -154,7 +159,9 @@ class Postprocessor(
await discard_file(extra_metadata_path(video_path), 'DEBUG') await discard_file(extra_metadata_path(video_path), 'DEBUG')
self._completed_files.append(result_path) self._completed_files.append(result_path)
await self._emit('video_postprocessing_completed', self, result_path) await self._emit(
'video_postprocessing_completed', self, result_path
)
except Exception as exc: except Exception as exc:
submit_exception(exc) submit_exception(exc)
finally: finally: