【问题标题】:PyV8 crashes with FlaskPyV8 与 Flask 一起崩溃
【发布时间】:2014-09-22 20:26:17
【问题描述】:

我正在尝试在 python (v2.7.3) 上使用 Flask (v0.10.1) 运行 PyV8(由 pip 安装,v1.0-dev),但应用程序在创建全局上下文时崩溃,没有办法知道出了什么问题,因为没有捕获到异常。 这是我的代码:

from flask import Flask, request, Response
import PyV8

try:
    from flask.ext.cors import CORS
except ImportError:
    import os
    parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    os.sys.path.insert(0, parentdir)

    from flask.ext.cors import CORS

class Global(PyV8.JSClass):     
   def hello(self):
        print 'Hello'

app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'

CORS(app)

@app.route('/', methods=['GET'])
def index():
    try:
        print 'got to the route'
        g = Global()
        print 'Global was created'
        ctxt = PyV8.JSContext(g) 
        print 'context was created'
        ctxt.enter()
        print 'context was entered'                   
        ctxt.eval("hello()")            
    except Exception as e:
        print 'error'
        print 'exception occurred, value:', e.value 

if __name__ == '__main__':
    app.run(host='0.0.0.0') 

在此应用程序崩溃之前向该应用程序发送 GET 时得到的输出是:

got to the route
Global was created

当我尝试在没有 Flask 的情况下运行 PyV8 时,它工作正常。 可能是什么原因?

【问题讨论】:

  • 你怎么知道它崩溃并且没有挂在ctxt = PyV8.JSContext(g)上?
  • 我在命令行运行,所以应用停止运行,返回命令行,端口空闲,需要重新运行。

标签: python flask pyv8


【解决方案1】:

我发现了导致问题的原因 - CORS。删除这部分后:

try:
    from flask.ext.cors import CORS
except ImportError:
    import os
    parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    os.sys.path.insert(0, parentdir)

    from flask.ext.cors import CORS

一切都按预期进行。我仍然不确定它导致崩溃的原因,这需要进一步调查,但我决定暂时不使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多