pythonpython-2.7matplotlibpipemultiprocessor

Sending data (BytesIO buffer) through a Pipe works but causes a Fatal Python exception


Using Python 2.7 on Windows, the following code works but causes a problem with msvc.

import io
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
from multiprocessing import Process, Pipe

def tmpPlot(conn):
  plt.plot([1,2,4,2])
  plt.title("title")

  buf = io.BytesIO()
  plt.savefig(buf, format='png')
  buf.seek(0)
  conn.send(plt.imread(buf))
  conn.close

if __name__ == '__main__':
  parent_conn, child_conn = Pipe()
  p = Process(target=tmpPlot, args=(child_conn,))
  p.start()

  imgData = parent_conn.recv()

  imgplt = plt2.imshow(imgData)

  plt2.show()

Here is the error:

MSVC Error

Followed by pythonw.exe has stopped working message:

  Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: pythonw.exe
  Application Version:  0.0.0.0
  Application Timestamp:    59bd8782
  Fault Module Name:    python27.dll
  Fault Module Version: 2.7.14150.1013
  Fault Module Timestamp:   59bd877e
  Exception Code:   40000015
  Exception Offset: 0014098b
  OS Version:   6.0.6002.2.2.0.768.3
  Locale ID:    1033
  Additional Information 1: ac4a
  Additional Information 2: 817fddeef0c50f6183a834229ec12634
  Additional Information 3: 3df9
  Additional Information 4: 47d9390827b4b422b52d14c0c48ab5d9

Read our privacy statement:
  http://go.microsoft.com/fwlink/?linkid=50163&clcid=0x0409

Solution

  • Adding a short sleep command seems to do the trick...

    conn.send(plt.imread(buf))
    time.sleep(.001)
    conn.close
    

    Not elegant but works.