Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

subprocess - If I launch a group of task to be executed in an event loop asyncio, can I react to the return of each separately?

I want to launch 10 OS subprocess with asyncio. I can do that with gather for example and then I can find out at the end of the event loop, the status of each tasks. But I have to wait for the whole thing to finish. Even when each task run concurrently. Is there a way to know that subprocess 1 already finished and react to that event, even before the other 9 tasks have completed?

I am working with Python >3.7 (3.8.6 and 3.9.1).

Maybe my question should be: Once that the event loop is running. Is there a way to find out the status of the tasks being running?

Or, the way it is expected that the task itself would do any after work after the await statement is completed but before returning and leaving the event loop.

I'll try that approach. In the meantime this is the code I am using for my basic testings:

Example of what I want:

import time


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

    stdout, stderr = await proc.communicate()
    if stdout:
      print(f'[stdout]
{stdout.decode()}')
    if stderr:
      print(f'[stderr]
{stderr.decode()}')
    return True





async def main():

    cmd00='sleep 35'
    cmd01='sleep 15'
    cmd02='sleep 25'
    cmd03='sleep 5'

    task0 = asyncio.create_task( osrunner(cmd00) )
    task1 = asyncio.create_task( osrunner(cmd01) )
    task2 = asyncio.create_task( osrunner(cmd02) )
    task3 = asyncio.create_task( osrunner(cmd03) )


    await task0
    await task1
    await task2
    await task3



print(f"started main at {time.strftime('%X')}")
asyncio.run(main())               #<------------------I want to poll the status of the tasks and do something while the others are still unfinished
print(f"finished main  at {time.strftime('%X')}")
question from:https://stackoverflow.com/questions/66055136/if-i-launch-a-group-of-task-to-be-executed-in-an-event-loop-asyncio-can-i-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...