S
S
Sergey Minakov2019-08-14 08:39:53
Python
Sergey Minakov, 2019-08-14 08:39:53

Why does Popen.wait() cause the subprocess to work incorrectly?

I'm writing a python program that needs to compile a .cpp file into an .exe and run it.
There is a MinGW compiler in the project folder, with the help of which I am just going to compile the program. When I run it with the Popen command in python (I used both the subprocess library and psutil) everything works fine.

proc = psutil.Popen('"./MinGW/compile_code_1.cmd"', shell=True, stdout=subprocess.PIPE)

But when I add proc.wait() to wait for compilation to finish, MinGW throws an error:

c:/users/cergm/desktop/unit tests/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a(main.o): In function ` main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `[email protected]'
collect2: ld returned 1 exit status

The same error occurs when I use subprocess.call() , proc.communicate() , and even
when I write while psutil.pid_exists(proc.pid) .
compile_code_1.cmd
@echo off
set PATH=%~dp0bin;%PATH%
title MinGW Portable Command Prompt
cd /d "%~dp0"
g++ ../solutions/code_1/main.cpp -o ../solutions/code_1/main
exit

main.cpp
#include <iostream>
#include <cstdio>

using namespace std;

int main(int narg, char*args[])
{
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  
  int a, b;
  
  cin >> a >> b;
  
  cout << a + b;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zanak, 2019-08-14
@Zanak

1. Your windows-gui application is compiling. Try explicitly looking towards the -m option for g++ and adding the correct value to your cmd script. There are no vents at hand, I can’t say more.
2. This function kind of runs your command with the specified parameters and waits for completion, doesn't it? (This is about waiting for completion)

S
Sergey Minakov, 2019-08-14
@cergmin

The problem is solved, it lies in the fact that before starting g ++ I was writing to the main.cpp file And the file simply did not update. To fix this you need to add
code_1_file.flush()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question