From 35adb8171e40c00b07d3143beed431d60b1dcac7 Mon Sep 17 00:00:00 2001 From: Dakota Blair Date: Fri, 1 Dec 2017 16:25:01 +0000 Subject: [PATCH 1/2] DOC: Updates multiprocessing example. --- ...multiprocess_sgskip.py => multiprocess.py} | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) rename examples/misc/{multiprocess_sgskip.py => multiprocess.py} (60%) diff --git a/examples/misc/multiprocess_sgskip.py b/examples/misc/multiprocess.py similarity index 60% rename from examples/misc/multiprocess_sgskip.py rename to examples/misc/multiprocess.py index eace2e05fd88..9a36674ceaab 100644 --- a/examples/misc/multiprocess_sgskip.py +++ b/examples/misc/multiprocess.py @@ -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): @@ -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() @@ -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() From 421831dc1671eaf0d04159571b504c6ac27b7be8 Mon Sep 17 00:00:00 2001 From: Dakota Blair Date: Fri, 1 Dec 2017 16:40:12 +0000 Subject: [PATCH 2/2] Restore multiprocessing example filename to skip doc generation per conversation with tacaswell. --- examples/misc/{multiprocess.py => multiprocess_sgskip.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/misc/{multiprocess.py => multiprocess_sgskip.py} (100%) diff --git a/examples/misc/multiprocess.py b/examples/misc/multiprocess_sgskip.py similarity index 100% rename from examples/misc/multiprocess.py rename to examples/misc/multiprocess_sgskip.py