enthought / mayavi

3D visualization of scientific data in Python
http://docs.enthought.com/mayavi/mayavi/
Other
1.3k stars 284 forks source link

Mayavi `mlab.animate` hangs with Multiprocessing #1153

Open Kchour opened 2 years ago

Kchour commented 2 years ago

I am doing some computation on one process and attempting to do 3d plotting on a second process. The idea is send data across a multiprocessing.Queue to the second process where a mlab.points3d object is modified using .set. However, I can't even get a basic animation working with multiprocessing.Process. Here is an MWE:

"""3d point animation using mayavi"""
import numpy as np
from multiprocessing import Queue, Process
from mayavi import mlab

class Plotter:

    def __init__(self) -> None:
        self.ball = mlab.points3d(np.array(1.), np.array(0.), np.array(0.))

    @mlab.animate(delay=10)
    def update(self):
        t = 0.0
        while True:
            # Process the self.queue here
            self.ball.mlab_source.set(x = np.cos(t), y = np.sin(t), z = 0)
            t+=0.1

            yield

    def show(self, queue):
        self.queue = queue
        self.update(self)
        mlab.show()

class Starter:

    def __init__(self) -> None:
        self.queue = Queue()
        self.plotter = Plotter()
        self.process = Process(target=self.plotter.show, args=(self.queue,), daemon=True)
        self.process.start()

    def submit(self, data):
        self.queue.put_nowait(data)

    def end(self):
        self.process.terminate()
        self.process.join()
        self.queue.close()

if __name__ == "__main__":
    # UNCOMMENT: The following works
    # plotter = Plotter()
    # plotter.update(plotter)
    # mlab.show()

    # Multiprocessing doesn't work :[
    starter = Starter()