プロが教える店舗&オフィスのセキュリティ対策術

python3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> flask.__version__
'2.2.2'
から、以下へ
fastapi 0.88.0

下記コードの移行したいと望んでいます

proc = subprocess.Popen( c, shell=True, stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line:
yield line
if not line and proc.poll() is not None:
break

flaskではプログラムの起動元にコード内で起動した
プロセスの出力が渡るのですがfastapiでは以下エラーがでます。
問題はどこかアドバイスいただけないでしょうか

ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/ueda/.local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/home/ueda/.local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/home/ueda/.local/lib/python3.9/site-packages/fastapi/applications.py", line 270, in __call__
await super().__call__(scope, receive, send)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
raise exc
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
File "/home/ueda/.local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/home/ueda/.local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/routing.py", line 706, in __call__
await route.handle(scope, receive, send)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/home/ueda/.local/lib/python3.9/site-packages/fastapi/routing.py", line 235, in app
raw_response = await run_endpoint_function(
File "/home/ueda/.local/lib/python3.9/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "/home/ueda/.local/lib/python3.9/site-packages/anyio/to_thread.py", line 31, in run_sync
return await get_asynclib().run_sync_in_worker_thread(
File "/home/ueda/.local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
return await future
File "/home/ueda/.local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 867, in run
result = context.run(func, *args)
File "/home/ueda/for_ssh_radiko/./radiko_fastapi.py", line 111, in audio
return Response(gen_sound())
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/responses.py", line 54, in __init__
self.body = self.render(content)
File "/home/ueda/.local/lib/python3.9/site-packages/starlette/responses.py", line 62, in render
return content.encode(self.charset)
AttributeError: 'generator' object has no attribute 'encode'

proc lineなどの変数が、str型で認識されているのかと
proc = b''
line = b''
の宣言してみましたが、エラーは発生します。

A 回答 (1件)

エラーメッセージによると、


>/home/ueda/for_ssh_radiko/./radiko_fastapi.py", line 111, in audio
return Response(gen_sound())
でエラーが発生してるので、ここらあたりがなんかあってないのでは。
    • good
    • 0
この回答へのお礼

なるほど、"/home/ueda/.local/lib/ のエラーばっかりだなーとながめていて見過ごしました
たしかに、fastapiの入門記事に、「デフォルトのResponseが制約あるんで、必要ならオーバーライドしな」記述がありました。
調べてみます

お礼日時:2023/02/05 21:28

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています