Environment:
Windows, Python 3.12
Problem Summary:
I have a Python application that utilizes a multiprocessing.Pool
to process a bunch of files in parallel:
proc_count = 16
with multiprocessing.Pool(proc_count) as pool:
result = pool.map_async(parse_func, logs)
# ... processing of result.get()
When run from the command line, everything is fine (no extra windows).
But when the app is launched with no console (from our launcher, a NodeJS Electron app that spawns the child process with shell=False
), a python.exe window pops up for each process (so 16 windows in this case).
I don’t want to set shell=True
; The app has a GTK4 front-end, and I don’t want a console window in addition to the app’s GUI.
Is there any exposed way to disable window creation by the multiprocessing.Pool
?
What Hasn’t Worked:
I have tried passing an initializer function to the Pool that attempts to hide the window, but it had no effect:
# Testing showed result = 0 below
# From Microsoft's docs: If the window was previously hidden, the return value is zero.
def initializer():
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)
if title.endswith('python.exe'):
result = win32gui.ShowWindow(window, win32con.SW_HIDE)
What Has Worked (but it’s bad):
I did find a way to make this work by setting the process creation flags (normally set to 0) in the multiprocessing library itself, but obviously I’m looking for a better solution.
From modified PythonDir/Lib/multiprocessing/popen_spawn_win32.py:
with open(wfd, 'wb', closefd=True) as to_child:
# start process
try:
CREATE_NO_WINDOW = 0x08000000 # <-- my temporary hack
hp, ht, pid, tid = _winapi.CreateProcess(
python_exe, cmd,
None, None, False, CREATE_NO_WINDOW, env, None, None) # <-- dwCreationFlags was 0 before