RuntimeError:主线程不在Matplotlib和Flask主循环中 [英] RuntimeError: main thread is not in main loop with Matplotlib and Flask

查看:422
本文介绍了RuntimeError:主线程不在Matplotlib和Flask主循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask,matplotlib保存图像,并使用tensorflow创建会话.运行以下代码时出现上述错误.烧瓶路由是否在单独的线程上运行?我怎样才能使fig.saveFig代码在Main线程上运行.非常感谢

I'm using flask, matplotlib to save image and tensorflow to create a session. I'm getting the above error when I run the following code. Does the flask route run on a separate thread? How can I make fig.saveFig piece of code run on the Main thread. Thanks a lot

 @app.route('/open', methods = ['GET', 'POST'])
 def sendOutput():
     global loss,a2,xP,yP,scale,sess,fig
     test_X,test_Y = own_model.getEvaluateData(scale)
     cost,ans = sess.run([loss,a2],feed_dict={xP:test_X,yP:test_Y})
     d = np.array(ans) - np.array(test_Y)
     val = hist(d,100)
     sess.close()
     fig.saveFig('abc.png') //Errror on this line

推荐答案

我当时处在相同的情况下,就是Flask和Matplotlib组合. 对我有用的是将Agg指定为Matplotlib后端.

I was on the same situation, Flask with Matplotlib combo. What worked for me is to specify Agg as Matplotlib backend.

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# Your code here

有关详细信息,请参阅Matplotlib文档(Web应用程序服务器中的Matplotlib).

You can refer to Matplotlib documentation (Matplotlib in a web application server) for the details.

这篇关于RuntimeError:主线程不在Matplotlib和Flask主循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆