zmq.error.ZMQError: Address already in use

1、如下代码,启动的时候python app.py会报如题的错误

app.py

#
!/user/bin python # -*- coding:utf-8 -*- import os from datetime import timedelta from flask import Flask, session, url_for, render_template, make_response, request, redirect import zerorpcimport multiprocessing APP = Flask(__name__) APP.secret_key = os.urandom(24) APP.permanent_session_lifetime = timedelta(seconds=30 * 24 * 60 * 60)
class RPCServer(object):
def hello(self, filename):
      return filename


@APP.route(
'/') @APP.route('/index') def index(): return render_template('index.html') @APP.route('/ok.htm',methods=['GET', 'POST','HEAD']) def ok(): return render_template('ok.htm') @APP.route('/test') def test(): session.permanent = True _str = '' if 'name' in session: _str = session['name'] if 'nick' in session: _str += '---' + session['nick'] #print session return _str def runWeb(): print ("start webapp") APP.run(host='0.0.0.0', port= 5000, debug=True, threaded=True) def runRPC(): #print "start rpc" s = zerorpc.Server(RPCServer()) s.bind("tcp://0.0.0.0:4242") s.run() if __name__ == "__main__": p1 = multiprocessing.Process(target = runWeb) p2 = multiprocessing.Process(target = runRPC) p1.start() p2.start() p1.join() p2.join() # p1.run() # p2.run() # p1.join() # p2.join()

2、错误信息:

3、查看进程情况:

4、lsof -i:4242 、lsof -i:5000查看端口情况,发现rpc服务并没有启动

5、但是如果关掉flask的debug模式,就没有问题

6、原因是:flask的debug模式会额外开启一个进程,这个进程负责监控代码是否发生变化,如果发生变化,会自动重启应用,使新修改代码立即自动生效;因此我猜想,是这个进程破坏了rpc服务的启动。

DEBUG模式下flask多开一个线程来监视项目的变化。

The first thing it does is start the main function in a new thread so it can monitor >the source files and restart the thread when they change.

参考自这篇文章http://stackoverflow.com/questions/9276078/whats-the-right-approach-for-calling-functions-after-a-flask-app-is-run

如果你想要避免加载两次,应该设置app.run(debug=True, use_reloader=False)

参考:

1、https://github.com/ipython/ipython/issues/6109

2、https://segmentfault.com/q/1010000000446372

3、

posted @ 2018-01-05 22:46  脚本小娃子  阅读(7801)  评论(0编辑  收藏  举报