Skip to content

Backport PR #9900 on branch v2.1.0-doc #9908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions examples/misc/multiprocess_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@
Multiprocess
============

Demo of using multiprocessing for generating data in one process and plotting
in another.
Demo of using multiprocessing for generating data in one process and
plotting in another.

Written by Robert Cimrman
"""

from __future__ import print_function
from six.moves import input

import time
from multiprocessing import Process, Pipe
import numpy as np

import matplotlib
matplotlib.use('GtkAgg')
from multiprocessing import Process, Pipe

# This example will likely not work with the native OSX backend.
# Uncomment the following lines to use the qt5 backend instead.
#
# import matplotlib
# matplotlib.use('qt5Agg')

import matplotlib.pyplot as plt
import gobject

# Fixing random state for reproducibility
np.random.seed(19680801)

###############################################################################
#
# Processing Class
# ================
#
# This class plots data it recieves from a pipe.
#


class ProcessPlotter(object):
def __init__(self):
Expand Down Expand Up @@ -55,18 +65,36 @@ def __call__(self, pipe):

self.pipe = pipe
self.fig, self.ax = plt.subplots()
self.gid = gobject.timeout_add(1000, self.poll_draw())
timer = self.fig.canvas.new_timer(interval=1000)
timer.add_callback(self.poll_draw())
timer.start()

print('...done')
plt.show()

###############################################################################
#
# Plotting class
# ==============
#
# This class uses multiprocessing to spawn a process to run code from the
# class above. When initialized, it creates a pipe and an instance of
# ``ProcessPlotter`` which will be run in a separate process.
#
# When run from the command line, the parent process sends data to the spawned
# process which is then plotted via the callback function specified in
# ``ProcessPlotter:__call__``.
#


class NBPlot(object):
def __init__(self):
self.plot_pipe, plotter_pipe = Pipe()
self.plotter = ProcessPlotter()
self.plot_process = Process(target=self.plotter,
args=(plotter_pipe,))
self.plot_process = Process(
target=self.plotter,
args=(plotter_pipe,)
)
self.plot_process.daemon = True
self.plot_process.start()

Expand All @@ -84,8 +112,8 @@ def main():
for ii in range(10):
pl.plot()
time.sleep(0.5)
input('press Enter...')
pl.plot(finished=True)


if __name__ == '__main__':
main()