簡體   English   中英

如何使用Popen同時運行幾個具有while(1)循環的程序

[英]How to run several programs that have a while(1) loop simultaneously using Popen

我想用Popen運行兩個C可執行文件。 他們兩個都有一個while(1)循環,我希望它們同時運行,但是我發現這樣做不行。

這是兩個C可執行文件:

int main(int argc, char *argv[]){
    char str1[20];
    int i = 0;
    while(i < 30){
        fprintf(stderr, "hello1\n");
        i++;
    }
    while(1);
}
int main(int argc, char *argv[]){
    char str1[20];
    int i = 0;
    while(i < 30){
        fprintf(stderr, "hello2\n");
        i++;
    }
    while(1);
}

這是python代碼:

processes=[subprocess.Popen(program,universal_newlines=True,shell=True) for program in ['./hello1', './hello2']]
for process in processes:
    process.wait()

它僅打印“ hello1”並掛起。

您的C代碼具有未引用的變量str1 ,即FYI。 雖然不是問題。

但是,在使用Visual C ++構建hello1.exehello2.exe並運行腳本之后,我沒有得到您的錯誤,因此看來您的問題是特定於您的安裝程序的。

#include "stdafx.h"
#include "stdio.h"

int main(int argc, char *argv[]) {
    int i = 0;
    while (i < 30) {
        fprintf(stderr, "hello[1/2]\n");
        i++;
    }
    while (1);
}

然后這將按您的預期工作:

import subprocess

processes = [subprocess.Popen(program, universal_newlines=True, shell=True) for program in ['hello1.exe', 'hello2.exe']]
for process in processes:
    process.wait()

它會同時打印hello1hello2 30次並等待。

暫無
暫無

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

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