[英]Communication between a Python parent and C child processes in Linux
我正在開發一個Python應用程序,該應用程序需要不時產生一個子進程(用C編寫),以為其提供一些二進制數據並獲得答復。 子進程僅在需要時才生成,並且只會處理一個請求。 我在這里有什么選擇? 使用stdin / stdout安全嗎?
from subprocess import Popen,PIPE
# Example with output only
p = Popen(["echo", "This is a test"], stdout=PIPE)
out, err = p.communicate()
print out.rstrip()
# Example with input and output
p = Popen("./TestProgram", stdin=PIPE, stdout=PIPE)
out, err = p.communicate("This is the input\n")
print out.rstrip()
程序TestProgram
從stdin
讀取一行並將其寫入stdout
。 我已經將.rstrip()
添加到輸出中,以刪除結尾的換行符,對於您的二進制數據,您可能不想這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.