fork(2) download
  1. #!/usr/bin/env python
  2. import matplotlib
  3. matplotlib.use('PDF') #NOTE: without it seqfault occures
  4.  
  5. import Queue
  6. import logging
  7. import math
  8. import numpy as np
  9. import sys
  10. import time
  11. import threading
  12.  
  13. from matplotlib import pyplot as plt
  14.  
  15. dbg = logging.debug
  16.  
  17. def plot_figure(queue, dqueue):
  18. while True:
  19. nfig = queue.get()
  20. try:
  21. dbg("plot %s" % nfig)
  22. x = np.arange(nfig*math.pi/100, 5+nfig*math.pi/100, 0.1);
  23. y = np.sin(x)
  24.  
  25. f = plt.figure(nfig)
  26. ax = f.add_subplot(111)
  27. ax.plot(x, y)
  28. ax.set_title(str(nfig))
  29. f.savefig('%s.pdf' % nfig)
  30. plt.close(nfig)
  31. queue.task_done()
  32. dbg("done %s" % nfig)
  33. finally:
  34. dqueue.put(nfig)
  35.  
  36. def main():
  37. logging.basicConfig(
  38. level=logging.DEBUG,
  39. format='%(asctime)s %(threadName)s %(message)s')
  40.  
  41. nthreads = int(sys.argv[1]) if len(sys.argv) > 1 else 2
  42. nfig = int(sys.argv[2]) if len(sys.argv) > 2 else 10
  43.  
  44. # start threads
  45. fqueue = Queue.Queue()
  46. dqueue = Queue.Queue()
  47. threads = [threading.Thread(target=plot_figure, args=(fqueue, dqueue))
  48. for fig in xrange(nthreads)]
  49.  
  50. dbg("start %d threads" % len(threads))
  51. for t in threads:
  52. t.daemon = True
  53. t.start()
  54.  
  55. dbg("fill queue")
  56. for fig in xrange(nfig):
  57. fqueue.put(fig)
  58.  
  59. dbg("wait for threads")
  60. for _ in xrange(nfig):
  61. dqueue.get()
  62. dbg("done")
  63.  
  64. if __name__=="__main__":
  65. main()
  66.  
  67.  
Success #stdin #stdout 0.01s 6656KB
stdin
Standard input is empty
stdout

Standard output is empty