[問題] subprocess問題

看板Python作者 (遥かなソラ)時間9年前 (2016/06/24 09:50), 9年前編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/2 (看更多)
手邊有一個python的GUI,另外開了一個C的subprocess 目前signal從python丟到C或是C丟到python都正常收到 但是資料的收送有些問題 Python Code: fpi_c = subprocess.Popen(["./FPI_sig"],stdin=subprocess.PIPE,stdout=subprocess.PIPE) def handle_inform_from_c(signum, frame): print("receved:", signum) outstr = fpi_c.stdout.read() print outstr signal.signal(signal.SIGUSR1, handle_inform_from_c) def gui_sense_and_save_fpi_2(): fpi_c.stdin.write(str(123)+"\n") os.kill(fpi_c.pid,signal.SIGUSR1) gui_sense_and_save_fpi_2接一個按鈕 按下去會送資料跟SIGUSR1給C程式 C code: void signal_callback_handler2(int signum) { int gpid; scanf("%d",&gpid); printf("Caught signal %d\n",signum); //fclose(stdout); signal(SIGUSR1, signal_callback_handler2); kill(getppid(),SIGUSR1); } int main() { signal(SIGUSR1, signal_callback_handler2); while(1) { sleep(0.5); } return EXIT_SUCCESS; } 目前如果加上fclose(stdout),python端就可以收到C print出來的"Caught signal XXX" 但是因為stdout close了 再傳資料就會顯示PIPE壞掉無法傳 沒有fclose(stdout)會當在python的outstr = fpi_c.stdout.read() 感覺好像是python一直在等C執行結束才會收值 但是C只會一直等signal不會自己結束 所以就卡死? 或者是subprocess.PIPE的管理根本就不對? 昨天一整天都在找資料跟嘗試 還是無法搞定 怎樣才能讓資料正常收送 懇請高手幫忙m(_ _)m -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.160.153.142 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1466733015.A.72A.html

06/24 12:45, , 1F
你送完要 flush 不然可能會被 OS 做 buffering
06/24 12:45, 1F
目前在python端 fpi_c.stdin.write(str(123)+"\n") 之後加上 fpi_c.stdin.flush() 以及C printf 後加上 fflush(stdout) 目前還是當在同個地方 ※ 編輯: lionel20002 (203.160.152.158), 06/24/2016 13:09:42

06/24 14:56, , 2F
不要用stdout.read,改用communicate試試
06/24 14:56, 2F

06/24 15:17, , 3F
樓上的試過 目前找到一點端倪 等下自己回一篇
06/24 15:17, 3F

06/24 17:55, , 4F
ZMQ很好用的
06/24 17:55, 4F
文章代碼(AID): #1NR97NSg (Python)
討論串 (同標題文章)
文章代碼(AID): #1NR97NSg (Python)