| 1 | #!/usr/bin/env python3
|
| 2 | """
|
| 3 | Testing child process APIs on Windows
|
| 4 | """
|
| 5 |
|
| 6 | import subprocess
|
| 7 | import sys
|
| 8 |
|
| 9 |
|
| 10 | def RunPipeline(argv1, argv2, argv3):
|
| 11 | p1 = subprocess.Popen(argv1, stdout=subprocess.PIPE)
|
| 12 | p2 = subprocess.Popen(argv2, stdin=p1.stdout, stdout=subprocess.PIPE)
|
| 13 |
|
| 14 | p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits
|
| 15 |
|
| 16 | p3 = subprocess.Popen(argv3, stdin=p2.stdout)
|
| 17 | p2.stdout.close() # Allow p2 to receive a SIGPIPE if p3 exits
|
| 18 |
|
| 19 | # Wait for each process to complete and get exit codes
|
| 20 | c1 = p1.wait()
|
| 21 | c2 = p2.wait()
|
| 22 | c3 = p3.wait()
|
| 23 | return [c1, c2, c3]
|
| 24 |
|
| 25 |
|
| 26 | def SubprocessDemo():
|
| 27 | if sys.platform == 'win32':
|
| 28 | # find /c /v is like wc -l
|
| 29 | #a_list = [['dir', 'build'], ['find', 'sh'], ['find', '/c', '/v', '""']]
|
| 30 | a_list = [['dir', 'build'], ['find', 'sh'], ['find', 'o']]
|
| 31 |
|
| 32 | a_list2 = []
|
| 33 | for a in a_list:
|
| 34 | a_list2.append(['cmd.exe', '/c'] + a)
|
| 35 | print(a_list2)
|
| 36 |
|
| 37 | codes = RunPipeline(a_list2[0], a_list2[1], a_list2[2])
|
| 38 | print(codes)
|
| 39 | return
|
| 40 |
|
| 41 | codes = RunPipeline(['ls', 'build'], ['grep', 'sh'], ['grep', 'o'])
|
| 42 | print(codes)
|
| 43 |
|
| 44 | codes = RunPipeline(['ls', 'build'], ['sh', '-c', 'grep py; exit 42'],
|
| 45 | ['wc', '-l'])
|
| 46 | print(codes)
|
| 47 |
|
| 48 |
|
| 49 | SubprocessDemo()
|