Skip to content

Commit 491408c

Browse files
committed
Added Karen Tracey's threading_test.py.
svn path=/trunk/matplotlib/; revision=7012
1 parent d91610a commit 491408c

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

unit/threading_test.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#! /usr/bin/python
2+
"""
3+
Test by Karen Tracey for threading problem reported in
4+
http://www.mail-archive.com/matplotlib-devel@lists.sourceforge.net/msg04819.html
5+
and solved by JDH with svn r7008.
6+
"""
7+
8+
import os
9+
import threading
10+
import traceback
11+
12+
import numpy as np
13+
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
14+
from matplotlib.figure import Figure
15+
16+
thread_count = 8
17+
max_iterations = 50
18+
exception_raised = False
19+
20+
def png_thread(tn):
21+
png_fname = 'out%d.png' % tn
22+
vals = 100 + 15 * np.random.randn(10000)
23+
24+
i = 0
25+
excp = None
26+
global exception_raised
27+
while not exception_raised and i < max_iterations:
28+
i += 1
29+
png_f = open(png_fname, 'wb')
30+
31+
try:
32+
fig = Figure()
33+
ax = fig.add_subplot(111)
34+
ax.hist(vals, 50)
35+
FigureCanvas(fig).print_png(png_f)
36+
37+
except Exception, excp:
38+
pass
39+
40+
png_f.close()
41+
if excp:
42+
print 'png_thread %d failed on iteration %d:' % (tn, i)
43+
print traceback.format_exc(excp)
44+
exception_raised = True
45+
else:
46+
print 'png_thread %d completed iteration %d.' % (tn, i)
47+
48+
os.unlink(png_fname)
49+
50+
def main(tc):
51+
threads = []
52+
for i in range(tc):
53+
threads.append(threading.Thread(target=png_thread, args=(i+1,)))
54+
55+
for t in threads:
56+
t.start()
57+
58+
for t in threads:
59+
t.join()
60+
61+
if not exception_raised:
62+
msg = 'Success! %d threads completed %d iterations with no exceptions raised.'
63+
else:
64+
msg = 'Failed! Exception raised before %d threads completed %d iterations.'
65+
66+
print msg % (tc, max_iterations)
67+
68+
if __name__== "__main__":
69+
main(thread_count)
70+

0 commit comments

Comments
 (0)