■ Popen 클래스를 사용해 자식 프로세스의 실행 결과를 다른 자식 프로세스의 입력 데이터로 연결해 병렬 프로세스 체인을 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import subprocess import os def runOpenSSL(sourceBytes): environmentDictionary = os.environ.copy() environmentDictionary["OPENSSL_PASSWORD"] = b"\xe24U\n\xd0Ql3S\x11" popen = subprocess.Popen( ["openssl", "enc", "-pbkdf2", "-pass", "env:OPENSSL_PASSWORD"], env = environmentDictionary, stdin = subprocess.PIPE, stdout = subprocess.PIPE ) popen.stdin.write(sourceBytes) popen.stdin.flush() # 자식 프로세스가 입력을 반드시 받도록 만든다. return popen def runSHA256(standardIN): popen = subprocess.Popen( ["sha256sum"], stdin = standardIN, stdout = subprocess.PIPE ) return popen openSSLPopenList = [] sha256PopenList = [] for _ in range(3): sourceBytes = os.urandom(10) openSSLPopen = runOpenSSL(sourceBytes) openSSLPopenList.append(openSSLPopen) sha256Popen = runSHA256(openSSLPopen.stdout) sha256PopenList.append(sha256Popen) for openSSLPopen in openSSLPopenList: openSSLPopen.communicate() for sha256Popen in sha256PopenList: outputBytes, errorBytes = sha256Popen.communicate() print(outputBytes.strip()) """ b'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 -' b'72b3b41b832e862fdee205ab201b9d403c6fb9c2355a85612026cb81e755a259 -' b'b16fc91c26e4eb1569df3faa3a5c200eff16e82a079361bf75f9a3b48af57b90 -' """ |