Programing

Python-문자열을 하위 프로세스에 어떻게 전달합니까?

lottogame 2020. 3. 31. 08:14
반응형

Python-문자열을 하위 프로세스에 어떻게 전달합니까?


내가 다음을 수행하면 :

import subprocess
from cStringIO import StringIO
subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one\ntwo\nthree\nfour\nfive\nsix\n')).communicate()[0]

나는 얻다:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 533, in __init__
    (p2cread, p2cwrite,
  File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 830, in _get_handles
    p2cread = stdin.fileno()
AttributeError: 'cStringIO.StringI' object has no attribute 'fileno'

분명히 cStringIO.StringIO 객체는 하위 프로세스에 맞게 파일 덕에 충분히 근접하지 않습니다. 이 문제를 어떻게 해결합니까?


Popen.communicate() 선적 서류 비치:

프로세스의 stdin에 데이터를 보내려면 stdin = PIPE를 사용하여 Popen 오브젝트를 작성해야합니다. 마찬가지로 결과 튜플에서 None 이외의 값을 얻으려면 stdout = PIPE 및 / 또는 stderr = PIPE도 제공해야합니다.

os.popen * 교체

    pipe = os.popen(cmd, 'w', bufsize)
    # ==>
    pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin

경고 자식 프로세스를 채우고 차단하는 다른 OS 파이프 버퍼로 인한 교착 상태를 피하려면 stdin.write (), stdout.read () 또는 stderr.read () 대신 communi ()을 사용하십시오.

따라서 다음과 같이 예제를 작성할 수 있습니다.

from subprocess import Popen, PIPE, STDOUT

p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)    
grep_stdout = p.communicate(input=b'one\ntwo\nthree\nfour\nfive\nsix\n')[0]
print(grep_stdout.decode())
# -> four
# -> five
# ->

현재 Python 3 버전에서는을 사용 subprocess.run하여 입력을 문자열로 외부 명령에 전달하고 종료 상태를 얻고 출력을 문자열로 다시 한 번의 호출로 얻을 수 있습니다.

#!/usr/bin/env python3
from subprocess import run, PIPE

p = run(['grep', 'f'], stdout=PIPE,
        input='one\ntwo\nthree\nfour\nfive\nsix\n', encoding='ascii')
print(p.returncode)
# -> 0
print(p.stdout)
# -> four
# -> five
# -> 

이 해결 방법을 찾았습니다.

>>> p = subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
>>> p.stdin.write(b'one\ntwo\nthree\nfour\nfive\nsix\n') #expects a bytes type object
>>> p.communicate()[0]
'four\nfive\n'
>>> p.stdin.close()

더 좋은 것이 있습니까?


아무도 파이프를 만들 것을 제안한 사람이 조금 놀랐습니다. 제 생각에는 문자열을 하위 프로세스의 stdin에 전달하는 가장 간단한 방법입니다.

read, write = os.pipe()
os.write(write, "stdin input here")
os.close(write)

subprocess.check_call(['your-command'], stdin=read)

Python 3.4 이상을 사용하는 경우 아름다운 솔루션이 있습니다. bytes 인수를 허용하는 input인수 대신 인수를 사용하십시오 stdin.

output = subprocess.check_output(
    ["sed", "s/foo/bar/"],
    input=b"foo",
)

나는 python3을 사용하고 있으며 stdin에 전달하기 전에 문자열을 인코딩해야한다는 것을 알았습니다.

p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = p.communicate(input='one\ntwo\nthree\nfour\nfive\nsix\n'.encode())
print(out)

"분명히 cStringIO.StringIO 객체는 하위 프로세스에 적합하도록 파일 덕에 충분히 근접하지 않습니다."

:-)

난 두려워하지. 파이프는 저수준 OS 개념이므로 OS 수준 파일 설명 자로 표시되는 파일 개체가 절대적으로 필요합니다. 해결 방법이 옳습니다.


from subprocess import Popen, PIPE
from tempfile import SpooledTemporaryFile as tempfile
f = tempfile()
f.write('one\ntwo\nthree\nfour\nfive\nsix\n')
f.seek(0)
print Popen(['/bin/grep','f'],stdout=PIPE,stdin=f).stdout.read()
f.close()

"""
Ex: Dialog (2-way) with a Popen()
"""

p = subprocess.Popen('Your Command Here',
                 stdout=subprocess.PIPE,
                 stderr=subprocess.STDOUT,
                 stdin=PIPE,
                 shell=True,
                 bufsize=0)
p.stdin.write('START\n')
out = p.stdout.readline()
while out:
  line = out
  line = line.rstrip("\n")

  if "WHATEVER1" in line:
      pr = 1
      p.stdin.write('DO 1\n')
      out = p.stdout.readline()
      continue

  if "WHATEVER2" in line:
      pr = 2
      p.stdin.write('DO 2\n')
      out = p.stdout.readline()
      continue
"""
..........
"""

out = p.stdout.readline()

p.wait()

부모 프로세스가 자식 하위 프로세스 forking 하기 전에 버퍼링하기 때문에 너무 큰 Popen.communicate(input=s)경우 문제 일으킬 수 있음을 명심하십시오. 즉 , 해당 시점에서 최소한 "두 배"의 사용 된 메모리가 필요하기 때문입니다 (적어도 "두건 아래"설명에 따름). 링크 된 문서는 여기에 있습니다 ). 내 특별한 경우에는 처음에 완전히 확장 된 다음 생성 된 생성기가 있었 으므로 자식 프로세스가 생성되기 직전에 부모 프로세스가 엄청 났으며 포크 할 메모리가 없었습니다.ssstdin

File "/opt/local/stow/python-2.7.2/lib/python2.7/subprocess.py", line 1130, in _execute_child self.pid = os.fork() OSError: [Errno 12] Cannot allocate memory


p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)    
p.stdin.write('one\n')
time.sleep(0.5)
p.stdin.write('two\n')
time.sleep(0.5)
p.stdin.write('three\n')
time.sleep(0.5)
testresult = p.communicate()[0]
time.sleep(0.5)
print(testresult)

참고 URL : https://stackoverflow.com/questions/163542/python-how-do-i-pass-a-string-into-subprocess-popen-using-the-stdin-argument

반응형