我看到的教程使用以下代码来运行服务器:
if __name__ == '__main__':
socketio.run(app)
我的__init__.py
文件是:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
from sqlalchemy import *
from flask.ext.socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)
app.debug = True
engine = create_engine('mysql://root:my_pw@localhost/db_name')
DBSession = sessionmaker(bind=engine)
import couponmonk.views
我的views.py
文件包含所有@app.route
和@socketio
装饰器。
我的问题是,我应该在哪里放置代码:
socketio.run(app)
当我把它放在__init__.py_
文件中时,我收到错误:
File "/opt/lampp/htdocs/flaskapp/flask.wsgi", line 7, in <module>
from couponmonk import app as application
File "/home/giri/Desktop/couponmonk/venv/couponmonk/__init__.py", line 14, in <module>
socketio.run(app)
File "/home/giri/Desktop/couponmonk/venv/lib/python2.7/site-packages/flask_socketio/__init__.py", line 411, in run
run_with_reloader(run_server)
File "/home/giri/Desktop/couponmonk/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 632, in run_with_reloader
return run_with_reloader(*args, **kwargs)
File "/home/giri/Desktop/couponmonk/venv/lib/python2.7/site-packages/werkzeug/_reloader.py", line 231, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
SystemExit: 2