我一直收到这个错误,只有当我部署我的图像并从 Google Cloud Run 运行它时。
[Errno 2] No such file or directory '/tmp/my_file_20230319_163330.mp3'
我的问题是,如何将新文件存储在 docker 容器中以便稍后调用?我试着把它放在 /tmp 中,但它似乎无法在那里找到它。那里不保存吗?
代码:
def create_audio(text):
audio_length = 120 * 15
file_name = None
text = text
overlay_type = None
# best is 10
overlay_volume = 15
# default rate is 200
# best is 300
rate = 250
speaker = None
text_mp3_file = create_file(text, rate, speaker)
audio_file = mix_audios(text_mp3_file, audio_length, file_name, overlay_type, overlay_volume)
return send_file(audio_file, as_attachment=True)
def create_file(text, rate, speaker):
engine = pyttsx3.init()
file_name = f'/tmp/my_file_{datetime.now().strftime(DATE_FORMAT)}.mp3'
engine.save_to_file(text, file_name)
engine.runAndWait()
engine.stop()
return file_name
def mix_audios(txt_file, audio_length, filename, overlay_type, overlay_volume):
text_mp3_file = AudioSegment.from_file(txt_file)
overlay = AudioSegment.from_file(overlay_file(overlay_type))
multiplier = math.ceil(audio_length / overlay.duration_seconds)
overlay = overlay * multiplier
overlay = overlay[:(audio_length * 1000)]
combined = overlay.overlay(text_mp3_file, gain_during_overlay=overlay_volume, loop=True)
file_name = f'/tmp/my_file_{datetime.now().strftime(DATE_FORMAT)}.mp3'
combined.export(file_name, format='mp3')
return file_name
违规行是
text_mp3_file = AudioSegment.from_file(txt_file)
找不到保存在这里的文件:
file_name = f'/tmp/my_file_{datetime.now().strftime(DATE_FORMAT)}.mp3'
app.yaml:
runtime: python310
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3.10
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Docker文件:
FROM python:3.10-slim
ENV PYTHONUNBUFFERED True
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
RUN python3 --version
RUN apt-get update && apt-get upgrade -y
RUN apt-get install espeak -y
RUN apt-get install ffmpeg -y
RUN apt-get install gunicorn -y
RUN apt-get install libespeak1 -y
RUN apt-get install portaudio19-dev -y
RUN apt-get install python3-pyaudio -y
RUN echo "Installed dependencies"
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
RUN pip list
CMD gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
这在本地对我有用
那里正在节省。 然而,Cloud Run 具有autoscaling。因此,并非所有请求都会转到与最近保存的文件相同的实例。
尽管您的 app.yaml 中有
manual_scaling
,但 Google Cloud Run 不支持手动缩放。如果您更喜欢手动缩放,可以改用 Google App Engine。
可以在下面找到有关 Cloud Run 与 Google App Engine 的更多信息: https://cloud.google.com/appengine/docs/flexible/cloud-run-for-gae-customers#comparison_summary
因此,由于 Cloud Run 的自动缩放,这将导致 FileNotFoundError。 此外,由于 Cloud Run 缩减,不活动的实例将被销毁并且您最近保存的文件将丢失。
您可以通过执行以下操作之一解决此问题: