Aaron Gonzalez
Aaron Gonzalez

Reputation: 106

Multiprocessing with Matplotlib Animation

In short, I am trying to run a live monitoring dashboard while calculating data in the background. Here is the code for my monitoring dashboard:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
import psutil
import collections

# function to update the data
def monitor_machine_metrics(i):
    cpu_metrics.popleft()
    cpu_metrics.append(psutil.cpu_percent(interval=1))
    ram_metrics.popleft()
    ram_metrics.append(psutil.virtual_memory().percent)

    # clear axis
    ax.cla()
    ax1.cla()

    # plot cpu usage
    ax.plot(cpu_metrics)
    ax.text(len(cpu_metrics)-1, cpu_metrics[-1]+2, "{}%".format(cpu_metrics[-1]))
    ax.set_ylim(0,100)

    # plot memory usage
    ax1.plot(ram_metrics)
    ax1.text(len(ram_metrics)-1, ram_metrics[-1]+2, "{}%".format(ram_metrics[-1]))
    ax1.set_ylim(0,100)
    legend = plt.legend(['CPU Usage', 'RAM Usage'], loc='upper left')
    return legend

# start collections with zeros
cpu_metrics = collections.deque(np.zeros(10))
ram_metrics = collections.deque(np.zeros(10))
usage_fig = plt.figure()
ax = plt.subplot()
ax1 = plt.subplot()

def display_dynamic_data(figure, funct, time_interval):
    ani = FuncAnimation(figure, funct, interval=time_interval)
    plt.show()

For the sake of keeping this simple, let's pretend I just want to print hi while the dashboard is running.

if __name__ == '__main__': 
    machine_monitoring_process = 
    multiprocessing.Process(target=display_dynamic_data,args=(usage_fig, monitor_machine_metrics, 1000))
    machine_monitoring_process.start()

    print('Hi')

Since FuncAnimation runs similar to an infinite loop, I purposely designed for this sub-process to be completely decoupled. While 'hi' prints, the figure comes up but no points will be displayed. However, display_dynamic_data works just fine if we just call the function instead of instantiating it as a sub-process. Unfortunately, I need this to run in parallel, since the real code is doing computations in the background.

Is there something that I am missing here?

Upvotes: 0

Views: 728

Answers (1)

azelcer
azelcer

Reputation: 1533

When you use multiprocessing.Process, a new process with its own memory and objects is created. Code running in the new process has no effect in the original one. If you want to interchange information, you should use queues or other type of information exchange mechanism.

In your example, display_dynamic_data runs in the new process and exits. Remember that you must keep a reference to the FuncAnimation object to prevent it from being garbage collected.

You can check this answer to grasp what happens when you use multiprocessing.

Upvotes: 1

Related Questions