来源:https://help.aliyun.com/document_detail/374323.html?spm=a2c4g.406172.4.1.789a72e9XDXwC3&scm=20140722.H_374323._.ID_374323-OR_rec-V_1
本文介绍如何使用阿里云智能语音服务提供的Python SDK,包括SDK的安装方法及SDK代码示例。
多线程和多并发
在CPython中,由于存在全局解释器锁,同一时刻只有一个线程可以执行Python代码(虽然某些性能导向的库可能会去除此限制)。如果您想更好地利用多核心计算机的计算资源,推荐你使用multiprocessing或concurrent.futures.ProcessPoolExecutor。 如果你想要同时运行多个I/O密集型任务,则多线程仍然是一个合适的模型。
如果单解释器有太多线程,将会在线程间切换造成更多消耗,有可能会导致SDK出现错误。不建议使用超过200线程,推荐使用multiprocessing技术或者手动使用脚本创建多个解释器。
代码示例
- 本示例中将合成的音频保存在文件中,如果您需要播放音频且对实时性要求较高,建议使用流式播放,即边接收语音数据边播放,减少延时。
- 本示例中使用SDK内置的默认外网访问服务端URL,如果您使用阿里云上海地域的ECS,并需要通过内网访问服务端URL,请使用如下URL:
URL="wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
import time
import threading
import sys
import nls
URL="wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1"
TOKEN="yourToken" #参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY="yourAppkey" #获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist
TEXT='大壮正想去摘取花瓣,谁知阿丽和阿强突然内讧,阿丽拿去手枪向树干边的阿强射击,两声枪响,阿强直接倒入水中'
#以下代码会根据上述TEXT文本反复进行语音合成
class TestTts:
def __init__(self, tid, test_file):
self.__th = threading.Thread(target=self.__test_run)
self.__id = tid
self.__test_file = test_file
def start(self, text):
self.__text = text
self.__f = open(self.__test_file, "wb")
self.__th.start()
def test_on_metainfo(self, message, *args):
print("on_metainfo message=>{}".format(message))
def test_on_error(self, message, *args):
print("on_error args=>{}".format(args))
def test_on_close(self, *args):
print("on_close: args=>{}".format(args))
try:
self.__f.close()
except Exception as e:
print("close file failed since:", e)
def test_on_data(self, data, *args):
try:
self.__f.write(data)
except Exception as e:
print("write data failed:", e)
def test_on_completed(self, message, *args):
print("on_completed:args=>{} message=>{}".format(args, message))
def __test_run(self):
print("thread:{} start..".format(self.__id))
tts = nls.NlsSpeechSynthesizer(url=URL,
token=TOKEN,
appkey=APPKEY,
on_metainfo=self.test_on_metainfo,
on_data=self.test_on_data,
on_completed=self.test_on_completed,
on_error=self.test_on_error,
on_close=self.test_on_close,
callback_args=[self.__id])
print("{}: session start".format(self.__id))
r = tts.start(self.__text, voice="ailun")
print("{}: tts done with result:{}".format(self.__id, r))
def multiruntest(num=500):
for i in range(0, num):
name = "thread" + str(i)
t = TestTts(name, "tests/test_tts.pcm")
t.start(TEXT)
nls.enableTrace(True)
multiruntest(1)
发表回复