阿三先生 2018-12-12 11:17 采纳率: 0%
浏览 664
已结题

python中,用yield获取返回值会增加耗时吗?

voice = yield threads.deferToThread(self.read_voice_from_url, responses.get("audioPath"))

threads.deferToThread是twisted框架里的异步回调,但我要使用回调结果值,就用yield接收了回调结果。

self.read_voice_from_url 是一个耗时操作,单独运行只需要十几毫秒,当使用yield接收异步返回值时,并发时,时间竟然增加了几十倍,请问什么原因引起的,怎么解决?

  • 写回答

2条回答 默认 最新

  • oyljerry 2018-12-12 11:45
    关注

    yield threads.deferToThread 这样就是变成同步操作了,一直等到read_voice_from_url执行完的结果,然后responses.get("audioPath")进行处理了,才返回给你,时间自然变长了

    result = threads.deferToThread(self.read_voice_from_url, responses.get("audioPath"))

    ...
    // 做些别的事情 在需要返回的时候,再返回
    yield result

    评论

报告相同问题?