簡體   English   中英

Python子流程模塊-意外行為

[英]Python subprocess module - unexpected behavior

我需要使用stdin / stdout將C控制台程序(作為子進程)與Python接口。

C程序更多或更少:

    tmp = 0.0;  
    printf("\ninput>>");
    scanf_s("%f",&tmp);
    printf ("\ninput was: %f",tmp);

    tmp = 0.0;
    printf("\ninput>>");
    scanf_s("%f",&tmp);
    printf ("\ninput was: %f",tmp);

    tmp = 0.0;
    printf("\ninput>>");
    scanf_s("%f",&tmp);
    printf ("\ninput was: %f",tmp);

使用python子進程模塊,我需要從該程序讀取數據,寫一些東西,然后再次讀取,依此類推。 我使用以下代碼:

>>> p=subprocess.Popen(['C:\T.exe'],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
>>> o,i=communicate('123\n')

o的輸出為:

input>>
input was: 123.000000
input>>
input was: 0.000000
input>>
input was: 0.000000

我希望子進程等待輸入,直到另一個o,i = communicate()調用。 為什么在沒有任何輸入的情況下進入程序的結尾? 如何解決?

每個進程最多可以有一個對communicate()調用,因為communicate()等待子進程終止。 要反復從進程的標准流讀取和寫入,請使用Popen類的stdoutstdin屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM