在 google TTS (python) 中使用同一客户端发送多个连续的“synthesize_speech”请求时出现“无流删除”错误

问题描述

正如您在下面的代码中看到的,我已经初始化了一个 TTS 客户端并使用该客户端来合成所有请求的输出。这是给出错误。但是,如果我通过将代码 - client = texttospeech.TextToSpeechClient() 放在生成函数中来为每个请求初始化客户端,它工作正常。

from flask import Flask,request
from google.cloud import texttospeech


app = Flask(__name__)

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/json'

# Initialize client
client = texttospeech.TextToSpeechClient()

voice = texttospeech.VoiceSelectionParams(
    language_code="en-US",ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.LINEAR16
)

@app.route('/generate',methods=['GET'])
def generator():
    text = request.args.get('text')

    # Google TTS
    synthesis_input = texttospeech.SynthesisInput(text=text)
    response = client.synthesize_speech(input=synthesis_input,voice=voice,audio_config=audio_config)

    return 'some variable'

if __name__ == '__main__':
    app.run(debug=True,host = '0.0.0.0',port='8000',threaded=False)

现在,当我像这样发送多个请求时 -

for i in range(100):
    requests.get('ip:port/generate',{'text': 'some large text. ' * 200})

最初的 3-4 个请求效果很好。之后,一些请求开始收到内部错误 (500)。这是完整的回溯 -

Traceback (most recent call last):
  File "/some/path/lib/python3.8/site-packages/google/api_core/grpc_helpers.py",line 73,in error_remapped_callable
    return callable_(*args,**kwargs)
  File "/some/path/lib/python3.8/site-packages/grpc/_channel.py",line 923,in __call__
    return _end_unary_response_blocking(state,call,False,None)
  File "/some/path/lib/python3.8/site-packages/grpc/_channel.py",line 826,in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNKNowN
        details = "Stream removed"
        debug_error_string = "{"created":"@1614073936.608639390","description":"Error received from peer ipv4:74.125.142.95:443","file":"src/core/lib/surface/call.cc","file_line":1067,"grpc_message":"Stream removed","grpc_status":2}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 2464,in __call__
    return self.wsgi_app(environ,start_response)
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 2450,in wsgi_app
    response = self.handle_exception(e)
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 1867,in handle_exception
    reraise(exc_type,exc_value,tb)
  File "/some/path/lib/python3.8/site-packages/flask/_compat.py",line 39,in reraise
    raise value
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 2447,in wsgi_app
    response = self.full_dispatch_request()
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 1952,in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 1821,in handle_user_exception
    reraise(exc_type,line 1950,in full_dispatch_request
    rv = self.dispatch_request()
  File "/some/path/lib/python3.8/site-packages/flask/app.py",line 1936,in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/current/app/path/deploy.py",line 37,in video_generator
    response = client.synthesize_speech(input=synthesis_input,audio_config=audio_config)
  File "/some/path/lib/python3.8/site-packages/google/cloud/texttospeech_v1/services/text_to_speech/client.py",line 374,in synthesize_speech
    response = rpc(request,retry=retry,timeout=timeout,Metadata=Metadata,)
  File "/some/path/lib/python3.8/site-packages/google/api_core/gapic_v1/method.py",line 145,in __call__
    return wrapped_func(*args,**kwargs)
  File "/some/path/lib/python3.8/site-packages/google/api_core/grpc_helpers.py",line 75,in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc),exc)
  File "<string>",line 3,in raise_from
    # Permission is hereby granted,free of charge,to any person obtaining a copy
google.api_core.exceptions.UnkNown: None Stream removed

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...