2014-09-22 66 views
2

我试图在python(v2.7.3)上使用Flask(v0.10.1)运行PyV8(由pip,v1.0-dev安装),但应用程序在创建Global上下文时崩溃,无法知道发生了什么问题,因为没有发现异常。 这里是我的代码:PyV8与Flask崩溃

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 

当我试图不瓶运行PyV8它工作正常。 可能是什么原因?

+0

你如何知道它崩溃并且不挂在'ctxt = PyV8.JSContext(g)'上? – 2014-09-22 20:33:41

+0

我在命令行中运行它,所以应用程序停止运行,返回到命令行,端口是空闲的,我需要再次运行它。 – irenal 2014-09-23 08:11:00

回答

0

我发现是什么导致了这个问题 - 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 

一切按预期工作。我仍然不确定它造成这次事故的原因,这需要进一步调查,但我决定暂时不使用它。