Skip to content

Commit db16175

Browse files
dopplershiftMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #9900: DOC: Updates multiprocessing example.
1 parent b8e997f commit db16175

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

examples/misc/multiprocess_sgskip.py

+40-12
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,37 @@
33
Multiprocess
44
============
55
6-
Demo of using multiprocessing for generating data in one process and plotting
7-
in another.
6+
Demo of using multiprocessing for generating data in one process and
7+
plotting in another.
88
99
Written by Robert Cimrman
1010
"""
11-
1211
from __future__ import print_function
13-
from six.moves import input
1412

1513
import time
16-
from multiprocessing import Process, Pipe
1714
import numpy as np
1815

19-
import matplotlib
20-
matplotlib.use('GtkAgg')
16+
from multiprocessing import Process, Pipe
17+
18+
# This example will likely not work with the native OSX backend.
19+
# Uncomment the following lines to use the qt5 backend instead.
20+
#
21+
# import matplotlib
22+
# matplotlib.use('qt5Agg')
23+
2124
import matplotlib.pyplot as plt
22-
import gobject
2325

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

29+
###############################################################################
30+
#
31+
# Processing Class
32+
# ================
33+
#
34+
# This class plots data it recieves from a pipe.
35+
#
36+
2737

2838
class ProcessPlotter(object):
2939
def __init__(self):
@@ -55,18 +65,36 @@ def __call__(self, pipe):
5565

5666
self.pipe = pipe
5767
self.fig, self.ax = plt.subplots()
58-
self.gid = gobject.timeout_add(1000, self.poll_draw())
68+
timer = self.fig.canvas.new_timer(interval=1000)
69+
timer.add_callback(self.poll_draw())
70+
timer.start()
5971

6072
print('...done')
6173
plt.show()
6274

75+
###############################################################################
76+
#
77+
# Plotting class
78+
# ==============
79+
#
80+
# This class uses multiprocessing to spawn a process to run code from the
81+
# class above. When initialized, it creates a pipe and an instance of
82+
# ``ProcessPlotter`` which will be run in a separate process.
83+
#
84+
# When run from the command line, the parent process sends data to the spawned
85+
# process which is then plotted via the callback function specified in
86+
# ``ProcessPlotter:__call__``.
87+
#
88+
6389

6490
class NBPlot(object):
6591
def __init__(self):
6692
self.plot_pipe, plotter_pipe = Pipe()
6793
self.plotter = ProcessPlotter()
68-
self.plot_process = Process(target=self.plotter,
69-
args=(plotter_pipe,))
94+
self.plot_process = Process(
95+
target=self.plotter,
96+
args=(plotter_pipe,)
97+
)
7098
self.plot_process.daemon = True
7199
self.plot_process.start()
72100

@@ -84,8 +112,8 @@ def main():
84112
for ii in range(10):
85113
pl.plot()
86114
time.sleep(0.5)
87-
input('press Enter...')
88115
pl.plot(finished=True)
89116

117+
90118
if __name__ == '__main__':
91119
main()

0 commit comments

Comments
 (0)