我正在尝试在 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 时,它工作正常。可能是什么原因?