OS:Windows 10(主机)
处理器:8
的Python:3.6.6
pygame版本:1.9.4
“生成器”:cx_Freeze版本5.1.1
OS: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
,所以这只是事实。
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
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系统上的多个窗口。
以及为什么会发生?
答案 0 :(得分:3)
使用cx_Freeze为Windows创建exe并使用某种形式的多处理(例如multiprocessing
或concurrent.futures.ProcessPoolExecutor
)时,您需要引导exe进行多处理您要做的第一件事就是简单地通过在if __name__ == "__main__":
块中调用multiprocessing.freeze_support()
作为第一件事。