- 1 year ago
Pygame. Windows 10. Creates additional windows after loop.run_in_executor using ProcessPoolExecutor
In Pygame, when using loop.run_in_executor
with ProcessPoolExecutor
, you might encounter issues where additional windows are created on Windows 10. This behavior could be related to how Windows handles subprocesses and the ProcessPoolExecutor
. To prevent this, you can try the following:
Explicitly Close the Executor:
After theloop.run_in_executor
is executed, explicitly close theProcessPoolExecutor
. This will ensure that all subprocesses are terminated properly, and no additional windows are created.Here's an example of how you can do this:
pythonimport pygame
import concurrent.futures
pygame.init()
# Create your main Pygame window
screen = pygame.display.set_mode((800, 600))
def background_task():
# Your background task logic here
# ...
with concurrent.futures.ProcessPoolExecutor() as executor:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
executor.shutdown() # Explicitly close the executor before exiting
quit()
# Execute the background task in the process pool
future = loop.run_in_executor(None, background_task)
# Wait for the task to complete (optional)
result = loop.run_until_complete(future)
# Your game's rendering and logic here
# ...
pygame.display.flip()
Use ThreadPoolExecutor Instead:
If you still encounter issues with theProcessPoolExecutor
, consider using theThreadPoolExecutor
instead, as it might behave differently on Windows. TheThreadPoolExecutor
runs tasks in a pool of threads instead of separate processes.pythonimport pygame
import concurrent.futures
pygame.init()
# Create your main Pygame window
screen = pygame.display.set_mode((800, 600))
def background_task():
# Your background task logic here
# ...
with concurrent.futures.ThreadPoolExecutor() as executor:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
executor.shutdown() # Explicitly close the executor before exiting
quit()
# Execute the background task in the thread pool
future = loop.run_in_executor(None, background_task)
# Wait for the task to complete (optional)
result = loop.run_until_complete(future)
# Your game's rendering and logic here
# ...
pygame.display.flip()
By explicitly closing the executor and potentially using ThreadPoolExecutor
, you can prevent the creation of additional windows and manage your background tasks more efficiently.