Pygame.Windows 10. Создает дополнительные окна после loop.run_in_executor, используя ProcessPoolExecutor - PullRequest
0 голосов
/ 26 сентября 2018

Среда, в которой воспроизводятся проблемы:

ОС: Windows 10 (хост)
Процессор: 8
Python: 3.6.6
версия Pygame: 1.9.4
"строитель": cx_Freeze версия 5.1.1

Среда, в которой проблемы не воспроизводятся:

ОС: Ubuntu 14.04 (guest, virtualbox)
Процессор: 4
Python: 3.6.6
версия Pygame: 1.9.4
"строитель": версия cx_Freeze 5.1.1

Скрипт

import asyncio
import pygame
import multiprocessing

from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor


def init():
    pygame.init()
    screen = pygame.display.set_mode((900, 700), pygame.RESIZABLE)
    clock = pygame.time.Clock()
    return screen, clock


def fast_cpu_blocked():
    print("blocked is run")
    i = 1
    while 1:
        i += 1
        if i > 100000000:
            print("blocked is finished")
            return i


executor_threads = ThreadPoolExecutor(multiprocessing.cpu_count())
executor_processes = ProcessPoolExecutor(multiprocessing.cpu_count())


async def start():
    loop = asyncio.get_event_loop()
    cpu_run = False
    screen, clock = init()
    while 1:
        await loop.run_in_executor(None, clock.tick, 60)
        screen.fill((0, 0, 0))
        txt_surface = pygame.font.Font(None, 18).render(
            "FPS: {}".format(int(clock.get_fps())), True, pygame.Color('grey'))
        screen.blit(txt_surface, (0, 0))
        pygame.display.flip()
        if not cpu_run:
            print("RUN CPU TASK")
            cpu_run = True
            loop.run_in_executor(executor_processes, fast_cpu_blocked)
            print("FINISH CPU TASK")


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(start())

Проблема:

При выполнении loop.run_in_executor(executor_processes, fast_cpu_blocked)он создает несколько окон приложения (просто черное окно, без какого-либо визуализированного контекста).

Этого не происходит при использовании executor_threads вместо executor_processes.Но в любом случае мне нужно executor_processes, так что это просто факт.

Журналы в Windows:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
RUN CPU TASK
FINISH CPU TASK
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
blocked is run
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
blocked is finished

Журналы в Ubuntu:

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
RUN CPU TASK
FINISH CPU TASK
blocked is run
blocked is finished

Вопрос:

Как исправить / избежать / взломать создание нескольких окон в системе Windows.
А почему это происходит?

1 Ответ

0 голосов
/ 27 сентября 2018

При создании exe для Windows, используя cx_Freeze и используя некоторую форму мультипроцессинга (например, multiprocessing или concurrent.futures.ProcessPoolExecutor, вам нужно сначала загрузить exe для мультипроцессинга, как самое первоевы делаете. Вы делаете это, просто вызывая multiprocessing.freeze_support() как самое первое внутри блока if __name__ == "__main__":.

...