Python 如何作为create_subprocess_shell协同程序的一部分访问标准输出?

Python 如何作为create_subprocess_shell协同程序的一部分访问标准输出?,python,subprocess,python-asyncio,Python,Subprocess,Python Asyncio,我正在编写一个python脚本,它为数百个子文件夹中的每一个子文件夹调用3个shell脚本。这些脚本对AWS S3进行ls调用,但从服务器接收响应的速度很慢,因此我决定异步编写此代码 我已经为这个问题建模了一个更简单的版本,我为三个参与者中的每一个调用了三个shell脚本。为了理解如何异步调用shell脚本,我下面使用的shell脚本不会ping AWS,它们只是打印随机数。我只是尝试捕获randomList.sh的输出,并将其作为列表(或其他我稍后将决定的数据类型)返回 我的问题:如何访问脚本

我正在编写一个python脚本,它为数百个子文件夹中的每一个子文件夹调用3个shell脚本。这些脚本对AWS S3进行ls调用,但从服务器接收响应的速度很慢,因此我决定异步编写此代码

我已经为这个问题建模了一个更简单的版本,我为三个参与者中的每一个调用了三个shell脚本。为了理解如何异步调用shell脚本,我下面使用的shell脚本不会ping AWS,它们只是打印随机数。我只是尝试捕获randomList.sh的输出,并将其作为列表(或其他我稍后将决定的数据类型)返回

我的问题:如何访问脚本调用的输出

#randomList.sh

x=$(( ( RANDOM % 10 )  + 1 ))
echo "sleeping for $x for $1"
sleep $x
x=$(( ( RANDOM % 10 )  + 1 ))
echo "sleeping for $x for $1"
sleep $x
x=$(( ( RANDOM % 10 )  + 1 ))
echo "sleeping for $x for $1"
sleep $x


编辑,自己解决 请参阅callScript函数。请注意,我还将
rows=asyncio.run(getRows())
放入
main()
函数中,但这与解决方案无关

async def callScript(script, participant):
    call = "./" + script + " " + participant
    #I CHANGED THE FOLLOWING LINES
    proc = await asyncio.create_subprocess_shell(call, stdout=asyncio.subprocess.PIPE)
    stdout = await proc.communicate()
    output = stdout[0]
    output = output.decode('ascii')
    output = output.split()
    return output


async def callParticipant(participant):
    script1 = 'randomList.sh'
    script2 = 'randomList1.sh'
    script3 = 'randomList2.sh'
    scriptCalls = []
    scriptCalls.append(asyncio.create_task(callScript(script1, participant)))
    scriptCalls.append(asyncio.create_task(callScript(script2, participant)))
    scriptCalls.append(asyncio.create_task(callScript(script3, participant)))
    results = await asyncio.gather(*scriptCalls)
    return results

async def getRows():
    participants = ["1","2","3"]
    tasks = []
    for participant in participants:
        tasks.append(asyncio.create_task(callParticipant(participant)))
    rows = await asyncio.gather(*tasks)
    return rows

def main ():
    rows = asyncio.run(getRows())
    print(rows)

if __name__ == "__main__":
    main()

您需要使用
wait
功能

-- your code until --
rows = asyncio.run(getRows())
result = await rows
您可以在以下网站上阅读更多关于it如何工作的信息:

此示例运行良好:

async def get_chat_id(name):
    await asyncio.sleep(3)
    return "chat-%s" % name

async def main():
    id_coroutine = get_chat_id("django")
    result = await id_coroutine

我不知道我可以在协同程序函数之外使用wait。实施修复后,我得到一个错误:
SyntaxError:'wait'外部函数
谢谢,但不是我想要的答案。我正在寻找有关异步子进程调用的帮助。我的解决方案张贴在上面。
async def get_chat_id(name):
    await asyncio.sleep(3)
    return "chat-%s" % name

async def main():
    id_coroutine = get_chat_id("django")
    result = await id_coroutine