FastApi에서 Asyncio 하위 프로세스를 실행하면 구현되지 않은 오류가 발생함
FastApi 경로로 를 실행하려고 하는데 실행 결과가 나타납니다. 이 문제에 대해 비슷한 질문을 읽어본 적이 있습니다:
Windows(윈도우)에서 비동기 및 대기 시 Not ImplementedError가 발생하는 이유는 무엇입니까?
Asyncio.create_subprocess_exec이 구현되지 않음 오류 - Fastapi 백그라운드 작업
하지만 그들은 실행 가능한 해결책을 갖고 있지 않은 것 같다.
내 FastApi 경로는 다음과 같습니다:
@app.get("/test/subprocess")
async def subprocess_test():
parsed_json = await(probe_video_file(Path(r"G:\ffmpeg testing\ffmpeg\ffprobe.exe"),
Path(r"G:\ffmpeg testing\input_file\test_file.wmv")))
print(parsed_json)
return parsed_json
이 경로로 이동하면 예외가 발생하고 코드가 으스러집니다. 함수 내부에는 다음과 같은 하위 프로세스 호출이 있습니다:
async def probe_video_file(ffprobe_path: Path, file_to_probe: Path) -> dict:
"""
Probes a video file with FFprobe.
:param ffprobe_path: Path to ffprobe executable
:param file_to_probe: Path to file to probe
:returns Parsed JSON dict of the output
"""
args = [f'{str(ffprobe_path)}', f'{str(file_to_probe)}']
args += ["-hide_banner", "-loglevel", "fatal", "-show_error", "-show_format", "-show_streams", "-show_chapters",
"-show_private_data", "-print_format", "json"]
#sub process is here, and here is where the exception happens.
proc = await asyncio.create_subprocess_exec(
*args,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
print(f'exited with {proc.returncode}]')
if stdout:
print(f'[stdout]\n{stdout.decode()}')
if stderr:
print(f'[stderr]\n{stderr.decode()}')
raise Exception(f"Failed to probe file {file_to_probe}")
return json.loads(stdout)
FastAPI를 사용하지 않고 실행할 경우 다음과 같습니다:
async def main():
json_output = await probe_video_file(Path(r"G:\ffmpeg testing\ffmpeg\ffprobe.exe"),
Path(r"G:\ffmpeg testing\input_file\test_file.wmv"))
print(json_output)
if __name__ == '__main__':
asyncio.run(main())
정상적으로 작동하며 올바른 출력을 출력합니다:
[['G:\\ffmpeg testing\\ffmpeg\\ffprobe.exe', 'G:\\ffmpeg testing\\input_file\\test_file.wmv', '-hide_banner', '-loglevel', 'fatal', '-show_error', '-show_format', '-show_streams', '-show_chapters', '-show_private_data', '-print_format', 'json'] exited with 0]
[stdout]
{
"streams": [
{
"index": 0,
"codec_name": "wmav2",
"codec_long_name": "Windows Media Audio 2",
"codec_type": "audio",
"codec_tag_string": "a[1][0][0]",
"codec_tag": "0x0161",
"sample_fmt": "fltp",
"sample_rate": "48000",
"channels": 2,
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/1000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 2155050,
"duration": "2155.050000",
"bit_rate": "96000",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "eng"
}
},
{
"index": 1,
"codec_name": "wmv3",
"codec_long_name": "Windows Media Video 9",
"profile": "Main",
"codec_type": "video",
"codec_tag_string": "WMV3",
"codec_tag": "0x33564d57",
"width": 850,
"height": 480,
"coded_width": 850,
"coded_height": 480,
"closed_captions": 0,
"has_b_frames": 0,
"pix_fmt": "yuv420p",
"level": -99,
"chroma_location": "left",
"refs": 1,
"r_frame_rate": "30000/1001",
"avg_frame_rate": "30000/1001",
"time_base": "1/1000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 2155050,
"duration": "2155.050000",
"bit_rate": "2000000",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"language": "eng"
}
}
],
"chapters": [
],
"format": {
"filename": "G:\\ffmpeg testing\\input_file\\test_file.wmv",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "asf",
"format_long_name": "ASF (Advanced / Active Streaming Format)",
"start_time": "0.000000",
"duration": "2155.050000",
"size": "567194391",
"bit_rate": "2105545",
"probe_score": 100,
"tags": {
"WMFSDKNeeded": "0.0.0.0000",
"DeviceConformanceTemplate": "MP@HL",
"WMFSDKVersion": "11.0.5721.5265",
"IsVBR": "0"
}
}
}
{'streams': [{'index': 0, 'codec_name': 'wmav2', 'codec_long_name': 'Windows Media Audio 2', 'codec_type': 'audio', 'codec_tag_string': 'a[1][0][0]', 'codec_tag': '0x0161', 'sample_fmt': 'fltp', 'sample_rate': '48000', 'channels': 2, 'bits_per_sample': 0, 'r_frame_rate': '0/0', 'avg_frame_rate': '0/0', 'time_base': '1/1000', 'start_pts': 0, 'start_time': '0.000000', 'duration_ts': 2155050, 'duration': '2155.050000', 'bit_rate': '96000', 'disposition': {'default': 0, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'eng'}}, {'index': 1, 'codec_name': 'wmv3', 'codec_long_name': 'Windows Media Video 9', 'profile': 'Main', 'codec_type': 'video', 'codec_tag_string': 'WMV3', 'codec_tag': '0x33564d57', 'width': 850, 'height': 480, 'coded_width': 850, 'coded_height': 480, 'closed_captions': 0, 'has_b_frames': 0, 'pix_fmt': 'yuv420p', 'level': -99, 'chroma_location': 'left', 'refs': 1, 'r_frame_rate': '30000/1001', 'avg_frame_rate': '30000/1001', 'time_base': '1/1000', 'start_pts': 0, 'start_time': '0.000000', 'duration_ts': 2155050, 'duration': '2155.050000', 'bit_rate': '2000000', 'disposition': {'default': 0, 'dub': 0, 'original': 0, 'comment': 0, 'lyrics': 0, 'karaoke': 0, 'forced': 0, 'hearing_impaired': 0, 'visual_impaired': 0, 'clean_effects': 0, 'attached_pic': 0, 'timed_thumbnails': 0}, 'tags': {'language': 'eng'}}], 'chapters': [], 'format': {'filename': 'G:\\ffmpeg testing\\input_file\\test_file.wmv', 'nb_streams': 2, 'nb_programs': 0, 'format_name': 'asf', 'format_long_name': 'ASF (Advanced / Active Streaming Format)', 'start_time': '0.000000', 'duration': '2155.050000', 'size': '567194391', 'bit_rate': '2105545', 'probe_score': 100, 'tags': {'WMFSDKNeeded': '0.0.0.0000', 'DeviceConformanceTemplate': 'MP@HL', 'WMFSDKVersion': '11.0.5721.5265', 'IsVBR': '0'}}}
Process finished with exit code 0
이벤트 루프를 다른 질문에 제시된 것처럼 '설정'해 보았습니다:
@app.on_event("startup")
async def startup_event():
"""Code runs at startup..."""
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
하지만 아무런 효과가 없었다.
다음은 예외 추적입니다:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "E:\pycharm\my_project\venv\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "E:\pycharm\my_project\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "E:\pycharm\my_project\venv\lib\site-packages\fastapi\applications.py", line 208, in __call__
await super().__call__(scope, receive, send)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__
await self.app(scope, receive, send)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\routing.py", line 259, in handle
await self.app(scope, receive, send)
File "E:\pycharm\my_project\venv\lib\site-packages\starlette\routing.py", line 61, in app
response = await func(request)
File "E:\pycharm\my_project\venv\lib\site-packages\fastapi\routing.py", line 226, in app
raw_response = await run_endpoint_function(
File "E:\pycharm\my_project\venv\lib\site-packages\fastapi\routing.py", line 159, in run_endpoint_function
return await dependant.call(**values)
File "E:\pycharm\my_project\src\backend\app\api\api.py", line 202, in subprocess_test
parsed_json = await(probe_video_file(Path(r"G:\ffmpeg testing\ffmpeg\ffprobe.exe"),
File "E:\pycharm\my_project\src\backend\app\ffmpeg\ffprobe.py", line 26, in probe_video_file
proc = await asyncio.create_subprocess_exec(
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
transport, protocol = await loop.subprocess_exec(
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1652, in subprocess_exec
transport = await self._make_subprocess_transport(
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 493, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError
INFO: 127.0.0.1:58576 - "GET /test/subprocess HTTP/1.1" 500 Internal Server Error
어떻게 해결해야 하는지 아는 사람? FastApi의 기본 이벤트 루프가 이를 지원하지 않는 것과 관련이 있는 것으로 알고 있습니다. 하지만 기본 FastApi 이벤트 루프를 Asyncio의 기본 이벤트 루프로 설정하거나 대체하는 방법을 모르겠습니다.
편집:
내가 윈도우 10을 사용하고 있다는 것을 지적하는 것이 중요하다고 생각한다.
유비콘 코드를 좀 더 가까이서 보면 옵션이 윈도우에서 기본값으로 변경되는 것 같다. 사용하지 않고 사용해 보시고 작동하는지 확인해보세요 (지금은 테스트가 불가능합니다. 그러나 uvicorn에서 특정 이벤트 루프의 사용을 강제해야 하는 경우 다음과 같이 서브클래스를 수행할 수 있습니다:
import asyncio
from asyncio.windows_events import ProactorEventLoop
from fastapi import FastAPI
from uvicorn import Config, Server
app = FastAPI()
class ProactorServer(Server):
def run(self, sockets=None):
loop = ProactorEventLoop()
asyncio.set_event_loop(loop) # since this is the default in Python 3.10, explicit selection can also be omitted
asyncio.run(self.serve(sockets=sockets))
config = Config(app=app, host="0.0.0.0", port=8000, reload=True)
server = ProactorServer(config=config)
server.run()
이를 통해 Fast 내에서 사용할 수 있습니다API 끝점.
나는 이와 같은 문제가 있었고 이것이 내 비밀번호이기도 하다는 것에 주목하여, 나는 uvicorn에서 인수를 제거하고 대신 직접 설치하고 uvicorn 명령어를 감쌌다. dev I에서 사용하는 예는 다음과 같습니다:
watchfiles "uvicorn mypackage.main:app" mypackage
그러면 폴더에서 코드 변경에 대한 감시가 시작됩니다.
이것은 watchfiles 프로세스가 이제 분리되어 있고 uvicorn이 더 이상 인수가 없기 때문에 기본 루프를 덮어쓰지 않기 때문에 Windows에서 작동합니다.
생산 시에는 재장전이 필요 없기 때문에 문제가 되지 않습니다.
'개발하자' 카테고리의 다른 글
테라폼에서 트리거 임계값과 메트릭_트리거 임계값의 차이 (1) | 2023.11.09 |
---|---|
Typescript 일반 JSX 인수와 함께 React forwardRef 사용 (0) | 2023.11.08 |
socket.io 을 fastapi 앱에 마운트하고 연결된 모든 클라이언트에 브로드캐스트를 전송하는 방법 (0) | 2023.11.07 |
"수출"을 패키지로 어떻게 사용할 수 있나요.json은 nested submodules와 TypeScript? (0) | 2023.11.07 |
이 Kubernetes 배포 환경에서 ErrrImagePull 오류가 발생하는 이유는 무엇입니까? (0) | 2023.11.06 |