CMake execute_process() doesn't print till program ends or stdout is flushed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.12) | |
project(stdout_buffering LANGUAGES CXX) | |
enable_testing() | |
set(CMAKE_CXX_STANDARD 11) | |
add_executable(print_flush print_flush.cpp) | |
add_test(NAME print_flush COMMAND print_flush) | |
find_package(Python COMPONENTS Interpreter) | |
if(Python_FOUND) | |
add_test(NAME print_flush_python | |
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/print_flush.py) | |
endif() | |
set_property(TEST print_flush print_flush_python PROPERTY TIMEOUT 10) | |
file(GENERATE OUTPUT .gitignore CONTENT "*") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Observe for Python flush=False, the printing is buffered till the first flush=True | |
cmake_minimum_required(VERSION 3.19) | |
find_package(Python COMPONENTS Interpreter REQUIRED) | |
execute_process(COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/print_flush.py | |
RESULT_VARIABLE ret | |
) | |
if(ret EQUAL 0) | |
message(STATUS "OK: print_flush") | |
else() | |
message(FATAL_ERROR "print_flush failed: ${ret}") | |
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://en.cppreference.com/w/cpp/io/manip/endl | |
// https://en.cppreference.com/w/cpp/io/cout | |
// https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio | |
#include <chrono> | |
#include <thread> | |
#include <iostream> | |
#include <cstdlib> | |
int main(){ | |
int milliseconds = 200, N = 6; | |
std::cout << "flush: True" << std::endl; | |
for (int i = 0; i < N; ++i) { | |
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); | |
std::cout << (float(i) / N) * 100 << " %" << std::endl; | |
} | |
std::cout << "flush: False\n"; | |
std::cout.sync_with_stdio(false); | |
// on some platforms, stdout flushes on \n | |
for (int i = 0; i < N; ++i) { | |
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); | |
std::cout << (float(i) / N) * 100 << " %\n"; | |
} | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
print() without flush and with flush | |
https://docs.python.org/3/library/functions.html#print | |
""" | |
from time import sleep | |
N = 6 # arbitrary number of prints | |
for flush in (True, False): | |
print("flush:", flush) | |
for i in range(N): | |
sleep(0.2) | |
print(f"{i/N*100:.1f} %", flush=flush) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment