【问题标题】:Cherrypy web server hangs forever -- Matplotlib errorCherrypy Web 服务器永远挂起 -- Matplotlib 错误
【发布时间】:2014-06-03 15:35:32
【问题描述】:

我正在为许多不同的命令行可执行文件创建一个基于 Web 的界面,并在 apache 后面使用cherrypy(使用 mod_rewrite)。我对此很陌生,并且很难正确配置东西。在我的开发机器上,一切正常,但是当我在第二台机器上安装代码时,我无法正常工作。

应用程序的基本工作流程是:1. 上传数据集,2. 处理数据(使用 python 并使用 subprocess.call 对可执行文件进行一些调用),3. 在网页上显示结果。

上传和处理一个数据集后,每次我尝试处理第二个数据集时,系统都会停止响应。我没有在cherrypy进程的终端中看到任何输出,也没有在显示任何错误的站点日志中看到任何输出。

我正在使用以下 conf 文件启动cherrypy:

[global]
environment: 'production'
log.error_file: 'logs/site.log'
log.screen: True
tools.sessions.on: True
tools.session.storage_type: "file"
tools.session.storage_path: "sessions/"
tools.sessions.timeout: 60
tools.auth.on: True
tools.caching.on: False
server.socket_host: '0.0.0.0'
server.max_request_body_size: 0 
server.socket_timeout: 60
server.thread_pool: 20
server.socket_queue_size: 10

engine.autoreload.on:True

我的 init.py 文件:

import cherrypy
import os
import string
from os.path import exists, join
from os import pathsep
from string import split

from mako.template import Template
from mako.lookup import TemplateLookup
from auth import AuthController, require, member_of, name_is

from twopoint import TwoPoint

current_dir = os.path.dirname(os.path.abspath(__file__))
lookup = TemplateLookup(directories=[current_dir + '/templates'])

def findInSubdirectory(filename, subdirectory=''):
  if subdirectory:
    path = subdirectory
  else:
    path = os.getcwd()
  for root, dirs, names in os.walk(path):
      if filename in names:
          return os.path.join(root, filename)
 return None

class Root:
  @cherrypy.expose
  @require()
  def index(self):
      tmpl = lookup.get_template("main.html")
      return tmpl.render(usr=WebUtils.getUserName(),source="")


if __name__=='__main__':
  conf_path = os.path.dirname(os.path.abspath(__file__))
  conf_path = os.path.join(conf_path, "prod.conf")
  cherrypy.config.update(conf_path)
  cherrypy.config.update({'server.socket_host': '127.0.0.1',
                          'server.socket_port': 8080});

  def nocache():
      cherrypy.response.headers['Cache-Control']='no-cache,no-store,must-revalidate'
      cherrypy.response.headers['Pragma']='no-cache'
      cherrypy.response.headers['Expires']='0'

  cherrypy.tools.nocache = cherrypy.Tool('before_finalize',nocache)
  cherrypy.config.update({'tools.nocache.on':'True'})

  cherrypy.tree.mount(Root(), '/')
  cherrypy.tree.mount(TwoPoint(),       '/twopoint')
  cherrypy.engine.start()
  cherrypy.engine.block()

对于发生这种情况的一个示例,我有以下调用我的 python 代码的 javascript 函数:

function compTwoPoint(dataset,orig){
  // call python code to generate images
  $.post("/twopoint/compTwoPoint/"+dataset,
   function(result){
       res=jQuery.parseJSON(result);
       if(res.success==true){
       showTwoPoint(res.path,orig);
       }
       else{
       alert(res.exception);
       $('#display_loading').html("");
       }
   });
}

这调用了python代码:

def twopoint(in_matrix):
   """proprietary code, can't share"""    

def twopoint_file(in_file_name,out_file_name):
    k = imread(in_file_name);
    figure()
    imshow(twopoint(k))
    colorbar()
    savefig(out_file_name,bbox_inches="tight")
    close()

class TwoPoint:

    @cherrypy.expose
    def compTwoPoint(self,dataset):
        try:
            fnames=WebUtils.dataFileNames(dataset)
             twopoint_file(fnames['filepath'],os.path.join(fnames['savebase'],"twopt.png"))

        return encoder.iterencode({"success": True})

这些功能协同工作以产生预期的结果。问题是处理一个输入文件后,我无法处理第二个文件。我似乎没有得到服务器的响应。

在运行正常的机器上,我正在运行 python 2.7.6 和 cherrypy 3.2.3。在第二台机器上,我有 python 2.7.7 和cherrypy 3.3.0。虽然这可以解释行为上的差异,但我想找到一种方法使我的代码具有足够的可移植性以克服版本差异(从旧到新)

我不确定问题是什么,甚至不知道要搜索什么。如果您能提供任何指导或帮助,我将不胜感激。

(编辑:再深入一点,我发现 matplotlib 发生了一些事情。如果我在 twopoint_file 中的 figure() 命令之前和之后放置打印语句,则只会打印第一个。直接从 python 解释器调用此函数(从等式中删除cherrypy)我收到以下错误:

无法调用“event”命令:应用程序在执行“event generate $w{{ThemeChanged}}”时被破坏 从“ttk::ThemeChanged”中调用的过程“ttk::ThemeChanged”第 6 行

结束编辑)

我不明白这个错误是什么意思,也没有太多的运气搜索。

【问题讨论】:

  • 显示所有代码,而不仅仅是配置文件。
  • 我目前有这个项目的 19 个不同的代码文件,看看什么最有帮助?
  • 主要开始怎么样。
  • 更新了问题以显示主文件,以及出现我的问题的一个案例的相关代码。
  • 进程运行多长时间?服务器是否比您的机器慢并超时?

标签: matplotlib cherrypy


【解决方案1】:

老问题,但我遇到了同样的问题,我通过在 Matplotlib 中更改后端解决了这个问题:

import matplotlib
matplotlib.use("qt4agg")

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多