16

I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.

When the user starts a simulation, and defines an initial state, I want the program to render the simulation continuously in the background, while the user may be doing different things in the program. Sort of like a YouTube-style bar that fills up: You can play the simulation only up to the point that was rendered.

How should I run the rendering function?

4

4 回答 4

10

我会使用 athreading.Thread在后台运行代码并将wx.CallAfter更新发布到我的窗口线程以将它们呈现给用户。

thread = threading.Thread(target=self.do_work)
thread.setDaemon(True)
thread.start()

...

def do_work(self):
    # processing code here
    while processing:
        # do stuff
        wx.CallAfter(self.update_view, args, kwargs)

def update_view(self, args):
    # do stuff with args
    # since wx.CallAfter was used, it's safe to do GUI stuff here
于 2009-04-14T02:36:37.927 回答
7

wxPython wiki 上有很多关于可能有用的长时间运行任务的信息。它们基本上使用线程并wx.PostEvent处理线程和主 wx 事件循环之间的通信。

于 2009-04-16T12:10:02.987 回答
4

Launch a new process to render in background and periodically check to see if it has returned.

You can find the documentation for the subprocess module here and the multiprocess module here. As Jay said, multiprocess is probably better if you're using Python 2.6. That said, I don't think there would be any performance difference between the two. Multiprocess just seems to be a wrapper around subprocess making certain things easier to do.

While subprocess/multiprocess is the standard way to do this, you may also want to take a look at Parallel Python.

于 2009-04-08T15:58:29.810 回答
0

如果您不介意使用稍微不同的方法,您可以查看stackless python 并为您的渲染过程创建一个 tasklet。我个人觉得它很容易使用。

于 2009-04-14T02:42:19.413 回答