代码之家  ›  专栏  ›  技术社区  ›  ClickThisNick

python 2龙卷风非同步方法

  •  2
  • ClickThisNick  · 技术社区  · 6 年前

    我必须使用python 2进行异步调用。

    我用龙卷风来完成这个任务,但是如果有更好的工具,我可以换工具。

    下面的代码运行 sleep 2 && echo hi 后台异步命令

    from tornado import gen
    import subprocess
    import time
    
    @gen.coroutine
    def longProcess():
        bashCommand = "sleep 5 && echo hi"
        process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
    
        yield output
    
    futures = [longProcess() for x in range(0, 5)]
    
    while True:
        if all(x.done() == True for x in futures):
            break
        time.sleep(1)
    
    print('All futures resolved')
    

    问题是 x.done() 正在回归 True 在我的bash命令完成之前的所有未来。

    我怎样才能转身 process.communicate() 进入未来(只做一次关键字“嗨”),这样我就可以等待所有的期货交易完成,然后得到期货的输出。

    1 回复  |  直到 6 年前
        1
  •  0
  •   xyres    6 年前

    使用龙卷风自己的 process.Subprocess 类,它是stdlib的包装器。 subprocess.Popen .

    例子:

    from tornado import process
    
    @gen.coroutine
    def longProcess():
        bashCommand = "sleep 5 && echo hi"
    
        proc = process.Subprocess(bashCommand.split(), stdout=subprocess.PIPE)
    
        yield proc.wait_for_exit() # `wait_for_exit` returns a future 
                                   # which you can yield. Basically, it means
                                   # you can wait for the process to complete
                                   # without blocking the server
    
        return proc.stdout.read() # return the result of the process
    
        # for Python 2:
        # you can't return from a generator
        # instead use this:
        # raise gen.Return(proc.stdout.read())
    

    你不需要 while 循环。您可以将代码移到其他的协同程序中,并生成列表。 futures . 这样地:

    @gen.coroutine
    def main():
        futures = [longProcess() for x in range(0, 5)]
    
        results = yield futures # this coroutine will not move further 
                                # until every future in the list 
                                # `futures` is resolved
    
        for result in results:
            print(result)
    
        print('All futures resolved')
    

    作为补充说明,不要使用 time.sleep . 它会阻塞整个服务器。而是使用异步等效 gen.sleep - yield gen.sleep(1)