1

Extending question asked here, Is there a way to find out what the command passed when creating a asyncio subprocess was?

Example Code :-

process = await asyncio.create_subprocess_shell(
                    command,
                    stdout=asyncio.subprocess.PIPE,
                    stderr=asyncio.subprocess.PIPE,
                )
print(process.arguments)
Alen Paul Varghese
  • 1,278
  • 14
  • 27

1 Answers1

2

asyncio.subprocess.Process does not store any information about command args. You can check __dict__ of the class.

Actually you know commandline args, since you provides them.

If you can control output of the called program I can offer the following walkaround which is composed on main.py and test.py. test.py sends back list of arguments which it received from main.py.

main.py file:

import asyncio


async def a_main():
    command_args = "test.py -n 1 -d 2 -s 3"  # your command args string
    process = await asyncio.create_subprocess_exec(
        "python",
        *command_args.split(),  # prepare args
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    print(type(process)) # asyncio.subprocess.Process
    # class instance do not store any information about command_args
    print(process.__dict__)
    stdout, stderr = await process.communicate()
    print(stdout)  # args are returned by our test script

if __name__ == '__main__':
    asyncio.run(a_main())

test.py file:

import sys

if __name__ == '__main__':
    print(sys.argv)
Artiom Kozyrev
  • 3,526
  • 2
  • 13
  • 31