简体   繁体   English

python 的并发异步问题

[英]Concurrency async issue with python

I am new to python, and my trying to resolve a simple issue: I have two blocks of operations (Block A and Block B), every block execute different shell commands that should be executed async inside the block (in the same block I can execute a new operation, without having to wait the result of the first one).我是 python 的新手,我试图解决一个简单的问题:我有两个操作块(块 A 和块 B),每个块执行不同的 shell 命令,这些命令应该在块内异步执行(在同一个块中我可以执行一个新操作,而不必等待第一个操作的结果)。

I can start running the operations of 'Block B' only when all the operations of 'Block A' are finished.只有在“Block A”的所有操作都完成后,我才能开始运行“Block B”的操作。

import asyncio
import subprocess
import sys
import threading


async def run_command(number, timeSleep):
    cmd = "(echo '"+ number +" Start -->' $(date) ;sleep "+timeSleep +" ;echo '"+ number +" End -->' $(date) ) >> /tmp/log.txt"

    p = subprocess.Popen(cmd, stderr=subprocess.PIPE, shell=True)

async def block_a():
    await asyncio.gather(
        run_command("Block A: Operation 1","8"),
        run_command("Block A: Operation 2","4"),
        run_command("Block A: Operation 3","2"))

async def block_b():
    await asyncio.gather(
        run_command("Block B Operation 1","5"),
        run_command("Block B Operation 2","4"),
        run_command("Block B Operation 3","1"))


async def main():
    await asyncio.gather(
        block_a(),
        block_b())

if __name__ == '__main__':
    # Create the asyncio event loop
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        # Shutdown the loop even if there is an exception
        loop.close()

I am getting the following output:我得到以下 output:

Block A: Operation 2 Start --> Thu Apr 9 21:21:27 CEST 2020
Block A: Operation 3 Start --> Thu Apr 9 21:21:27 CEST 2020
Block A: Operation 1 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 1 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 2 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 3 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 3 End --> Thu Apr 9 21:21:28 CEST 2020
Block A: Operation 3 End --> Thu Apr 9 21:21:29 CEST 2020
Block A: Operation 2 End --> Thu Apr 9 21:21:31 CEST 2020
Block B Operation 2 End --> Thu Apr 9 21:21:31 CEST 2020
Block B Operation 1 End --> Thu Apr 9 21:21:32 CEST 2020
Block A: Operation 1 End --> Thu Apr 9 21:21:35 CEST 2020

I am expecting to start operations of Block B after that operations of Block A are completed.我希望在 Block A 的操作完成后开始 Block B 的操作。
The output that I was expecting is something like:我期待的 output 是这样的:

Block A: Operation 2 Start --> Thu Apr 9 21:21:27 CEST 2020
Block A: Operation 3 Start --> Thu Apr 9 21:21:27 CEST 2020
Block A: Operation 1 Start --> Thu Apr 9 21:21:27 CEST 2020
Block A: Operation 3 End --> Thu Apr 9 21:21:29 CEST 2020
Block A: Operation 2 End --> Thu Apr 9 21:21:31 CEST 2020
Block A: Operation 1 End --> Thu Apr 9 21:21:35 CEST 2020
Block B Operation 1 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 2 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 3 Start --> Thu Apr 9 21:21:27 CEST 2020
Block B Operation 3 End --> Thu Apr 9 21:21:28 CEST 2020
Block B Operation 2 End --> Thu Apr 9 21:21:31 CEST 2020
Block B Operation 1 End --> Thu Apr 9 21:21:32 CEST 2020

You have to use the asyncio module to create subprocesses, rather than the subprocess module directly.您必须使用asyncio模块来创建子流程,而不是直接使用subprocess模块。
From the docs :文档

import asyncio

async def run(cmd):
    proc = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE)

    stdout, stderr = await proc.communicate()

    print(f'[{cmd!r} exited with {proc.returncode}]')
    if stdout:
        print(f'[stdout]\n{stdout.decode()}')
    if stderr:
        print(f'[stderr]\n{stderr.decode()}')

asyncio.run(run('ls /zzz'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM