通过管道发送数据(BytesIO缓冲区)有效,但会导致致命的Python异常

时间:2017-10-30 19:50:49

标签: python python-2.7 matplotlib pipe multiprocessor

在Windows上使用Python 2.7,以下代码可以正常工作,但会导致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()

这是错误:

MSVC Error

随后pythonw.exe已停止工作消息:

  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

1 个答案:

答案 0 :(得分:0)

添加短暂的睡眠命令似乎可以解决这个问题......

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

不优雅但有效。