gpt4 book ai didi

python - 游戏。 Windows 10。使用 ProcessPoolExecutor 在 loop.run_in_executor 之后创建额外的窗口

转载 作者:可可西里 更新时间:2023-11-01 09:57:27 29 4
gpt4 key购买 nike

问题重现环境:

操作系统:Windows 10(主机)
中央处理器:8
python :3.6.6
游戏版本:1.9.4
“构建器”:cx_Freeze 版本 5.1.1

问题未重现的环境:

操作系统:Ubuntu 14.04( guest ,虚拟机)
中央处理器:4
python :3.6.6
游戏版本: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 系统上生成多个窗口。
为什么会这样?

最佳答案

使用 cx_Freeze 为 Windows 创建 exe 时并使用某种形式的多处理(例如 multiprocessingconcurrent.futures.ProcessPoolExecutor 你需要引导你的 exe 进行多处理,这是你做的第一件事。您只需调用 multiprocessing.freeze_support() 作为 if __name__ == "__main__": block 中的第一件事即可完成此操作。

关于python - 游戏。 Windows 10。使用 ProcessPoolExecutor 在 loop.run_in_executor 之后创建额外的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52519568/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com