首页 > 解决方案 > 一次运行两个 python 文件

问题描述

我有两个 python 文件,一个在 pygame 中运行模拟,另一个获取数据并绘制图表。我希望它们在两个单独的窗口中运行,但我希望在模拟运行时记录数据。我已经尝试过多线程,但它似乎不起作用。

这两个函数都只是真正的循环,我希望它们并行运行。

def main():
    while True:
        clock.tick(simSpeed)

        # quit function
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        pendulum.update()
        drawWindow(screen, pendulum)

threading.Thread(target=main).start()
threading.Thread(target=graphData).start()
def graphData():
    while True:
        x = data.data["x"]
        y = data.data["y"]
        plot1 = plt.figure(1)
        plt.title("poopyhead")
        plt.plot(y, x)

        plot2 = plt.figure(5)
        plt.plot(x, y)

        plot1.clear()
        plot2.clear()

        plt.show()
        time.sleep(1)

标签: pythonmultithreadingparallel-processingmultiprocessing

解决方案


感谢大家的帮助。每次执行 while 循环时,我最终只是在同一个窗口中重新渲染图形。对我来说效果很好,我可以完全避免多线程。


推荐阅读