Matplotlib not responding when used with multiprocessing

I am trying to create a very basic application that will update charts in matplotlib from a streaming data source. Data is received in a separate process. But my matplotlib figure continues to die from me even for the most basic display. The matplotlib window loses interactivity and turns into "Figure 1 (Does not respond)" . Do I need to give matplotlib some processor time explicitly to get it working with multiprocessing?

Here is a basic example that dies in almost all backends in Windows 7, 64Bit, Python 2.7.3 32Bit. I am using unofficial scipy-stack binary from here :

EDIT: It also does not work on Ubuntu (32 bit).

import time

from multiprocessing import Process

import matplotlib.pyplot as plt

def p1_func():
    while True:
        time.sleep(1)


def p2_func():
    plt.ion()
    plt.plot([1.6, 2.7])
    while True:
        time.sleep(1)

if __name__ == '__main__':

    p1_proc = Process(target=p1_func)
    p2_proc = Process(target=p2_func)

    p1_proc.start()
    p2_proc.start()

    p1_proc.join()
    p2_proc.join()

?

matplotlib ( )?

+4
4

import time
from multiprocessing import Process, Pipe

import numpy as np
import matplotlib.pyplot as plt

class DataStreamProcess(Process):
    def __init__(self, connec, *args, **kwargs):
        self.connec = connec
        Process.__init__(self, *args, **kwargs)

    def run(self):
        random_gen = np.random.mtrand.RandomState(seed=127260)
        for _ in range(30):
            time.sleep(0.01)
            new_pt = random_gen.uniform(-1., 1., size=2)
            self.connec.send(new_pt)


def main():
    conn1, conn2  = Pipe()
    data_stream = DataStreamProcess(conn1)
    data_stream.start()

    plt.gca().set_xlim([-1, 1.])
    plt.gca().set_ylim([-1, 1.])
    plt.gca().set_title("Running...")
    plt.ion()

    pt = None
    while True:
        if not(conn2.poll(0.1)):
            if not(data_stream.is_alive()):
                break
            else:
                continue
        new_pt = conn2.recv()
        if pt is not None:
            plt.plot([pt[0], new_pt[0]], [pt[1], new_pt[1]], "bs:")
            plt.pause(0.001)
        pt = new_pt

    plt.gca().set_title("Terminated.")
    plt.draw()
    plt.show(block=True)

if __name__ == '__main__':
    main()
+3

matplotlib.animation multiprocessing.Pool ( ), , @deinonychusaur, .

:

import matplotlib
matplotlib.use('AGG')  # Do this BEFORE importing matplotlib.pyplot
import matplotlib.pyplot as plt

: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

+3

, pylab .

import pylab
from threading import Thread


def threaded_function(arg):
    pylab.plot(range(1,10))
    pylab.show(block=True)


if __name__ == "__main__":
    thread = Thread(target = threaded_function, args = (10, ))
    thread.start()
    thread.join()
    print("thread finished...exiting")
+1

join()awaiting completion of the process. In this case, the code expects an infinite loop :)

To build a @tcaswell comment, mixing a GUI loop and multiprocessor material is risky. First try this, not the code above:

   procs = [p1_proc, p2_proc]
   while any( (p.is_alive() for p in procs) ):
      time.sleep(1)
0
source

Source: https://habr.com/ru/post/1544062/


All Articles