【发布时间】: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)上? -
我在命令行运行,所以应用停止运行,返回命令行,端口空闲,需要重新运行。