简体   繁体   English

PyV8与Flask崩溃

[英]PyV8 crashes with Flask

I'm trying to run PyV8 (installed by pip, v1.0-dev) with Flask (v0.10.1) on python (v2.7.3) but the application crashes on creating the Global context, there is no way to know what went wrong because no exception is being caught. 我正在尝试在Python(v2.7.3)上使用Flask(v0.10.1)运行PyV8(由pip,v1.0-dev安装),但是应用程序在创建Global上下文时崩溃,无法知道发生了什么错误的,因为没有异常被捕获。 Here is my code: 这是我的代码:

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') 

The output I'm getting when firing a GET to this app before it crashes is: 在崩溃之前向该应用程序触发GET时,我得到的输出是:

got to the route
Global was created

When I'm trying to run an PyV8 without the Flask it works fine. 当我尝试在不使用Flask的情况下运行PyV8时,效果很好。 What may be the reason? 可能是什么原因?

I found out what was causing the problem - the CORS. 我发现了导致问题的原因-CORS。 After removing this part: 删除此部分后:

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

everything worked as expected. 一切都按预期进行。 I'm still not sure about the reason it made the crash, this needs further investigation, but I decided not to use it for now. 我仍然不确定造成崩溃的原因,这需要进一步调查,但是我决定暂时不使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM