I made a script which calls some program via os.system() inside a ThreadPool and prints results of work to console. Simplified code looks this way:
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
import multiprocessing
from threading import Lock
print_mutex = Lock()
def print_safe(text):
with print_mutex:
print(text, flush=True)
def print_smth():
res = os.system("")
print_safe("1")
with ThreadPoolExecutor(max_workers = multiprocessing.cpu_count()) as thread_pool:
future_results = [thread_pool.submit(print_smth) for i in range(40)]
for future in future_results:
future.result()
But in console output I receive unexpected result like that:
If I comment res = os.system("") call, then output becomes valid.
Why does os.system() influences such way on print function?
What is the correct way to implement process calls to avoid such issue?
