Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd93a2a9

Browse files
authored
Merge pull request#9900 from dakotablair/issue7388-multiprocessing-example
DOC: Updates multiprocessing example.
2 parentsdbe0502 +421831d commitd93a2a9

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

‎examples/misc/multiprocess_sgskip.py

Lines changed: 40 additions & 12 deletions
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+
plottingin another.
88
99
Written by Robert Cimrman
1010
"""
11-
1211
from __future__importprint_function
13-
fromsix.movesimportinput
1412

1513
importtime
16-
frommultiprocessingimportProcess,Pipe
1714
importnumpyasnp
1815

19-
importmatplotlib
20-
matplotlib.use('GtkAgg')
16+
frommultiprocessingimportProcess,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
importmatplotlib.pyplotasplt
22-
importgobject
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
classProcessPlotter(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
classNBPlot(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
foriiinrange(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)

[8]ページ先頭

©2009-2025 Movatter.jp