简体   繁体   中英

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. 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:

got to the route
Global was created

When I'm trying to run an PyV8 without the Flask it works fine. What may be the reason?

I found out what was causing the problem - the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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