我使用pygame来运行认知科学中的实验,而且我经常有繁重的I/O需求,所以我喜欢将这些任务分叉到单独的进程中(当使用多核机器时),以提高代码的性能。然而,我遇到了一个场景,其中一些代码可以在我同事的linux机器(Ubuntu LTS)上运行,但不能在我的mac上运行。下面的代码代表了一个最小的可重现示例。我的mac是一台2011年的Macbook Air,运行10.7.2,使用默认的python 2.7.1。我尝试了通过pre-built binary安装的这两个pygame,然后在从源代码安装了SDL和pygame之后也尝试了。
import pygame
import multiprocessing
pygame.init()
def f():
while True:
pygame.event.pump() #if this is replaced by pass, this code works
p = multiprocessing.Process(target=f)
p.start()
while True:
pass正如代码中所指出的,罪魁祸首似乎是将pygame.event.pump()放在一个单独的进程中。当我在我的mac上运行这个命令时,我首先会在终端中重复打印以下内容:
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.然后我得到了一份复制到this gist的崩溃报告。
对如何解决这个问题有什么建议吗?
发布于 2011-11-18 06:59:31
也许您应该在每个分支(子)进程中初始化pygame (初始化SDL-> OpenGL),如示例所示:
import multiprocessing
def f():
import pygame
pygame.init()
while True:
pygame.event.pump()
if __module__ == "__main__"
p = multiprocessing.Process(target=f)
p.start()
import pygame
pygame.init()
while True:
pygame.event.pump()发布于 2012-12-01 20:36:35
请尝试此链接:
http://www.slideshare.net/dabeaz/an-introduction-to-python-concurrency#btnPrevious
这可能会有所帮助。问题是您正在创建一个永远不会停止的进程。应将其声明为守护进程:
p = multiprocessing.Process(target=f)
p.daemon = True
p.start()不确定这是否能解决这个问题,我只是在写这篇文章的时候学习了多进程模块。
发布于 2011-11-17 07:44:05
您是否尝试过使用线程而不是进程?在使用OS X中的python多处理模块之前,我遇到过一些问题。http://docs.python.org/library/threading.html
https://stackoverflow.com/questions/8106002
复制相似问题