voice = yield threads.deferToThread(self.read_voice_from_url, responses.get("audioPath"))
threads.deferToThread是twisted框架里的异步回调,但我要使用回调结果值,就用yield接收了回调结果。
self.read_voice_from_url 是一个耗时操作,单独运行只需要十几毫秒,当使用yield接收异步返回值时,并发时,时间竟然增加了几十倍,请问什么原因引起的,怎么解决?
voice = yield threads.deferToThread(self.read_voice_from_url, responses.get("audioPath"))
threads.deferToThread是twisted框架里的异步回调,但我要使用回调结果值,就用yield接收了回调结果。
self.read_voice_from_url 是一个耗时操作,单独运行只需要十几毫秒,当使用yield接收异步返回值时,并发时,时间竟然增加了几十倍,请问什么原因引起的,怎么解决?
yield threads.deferToThread 这样就是变成同步操作了,一直等到read_voice_from_url执行完的结果,然后responses.get("audioPath")进行处理了,才返回给你,时间自然变长了
result = threads.deferToThread(self.read_voice_from_url, responses.get("audioPath"))
...
// 做些别的事情 在需要返回的时候,再返回
yield result