我应该在相同的端口上运行普通的flask应用程序和套接字吗?

时间:2018-07-04 13:30:05

标签: python nginx flask gunicorn eventlet

我正在通过gunicorn,eventlet和nginx将模块index.py作为烧瓶应用程序运行。

代码运行正常,但是我无法使其在规定的时间内运行。

这是代码:

from __future__ import print_function
import ast
from collections import defaultdict
import csv
import datetime
from itertools import chain
import json
import os
import operator
import sys
import time
import pymongo
from pymongo import MongoClient
from flask import Flask, render_template, redirect, url_for, request
from flask_cors import CORS
from flask_socketio import SocketIO, emit

async_mode = None

app = Flask(__name__)
app.secret_key = os.urandom(24)
socketio = SocketIO(app)
CORS(app)

sys.path.insert(0, '/home/aviral/dev/bubble-process-watson')
from entity_dict import entity_dict_CRO, entity_dict_DEN, entity_dict_url_CRO, entity_dict_url_DEN

entityDict = defaultdict(list)
for k, v in chain(entity_dict_url_CRO.items(), entity_dict_url_DEN.items()):
    entityDict[k].append(v)

try:
    client = MongoClient('localhost', 27017)
    db = client["Bubble"]
except Exception as e:
    print(e)

start_match = datetime.datetime.strptime(
    "2018-07-01 18:00:00", '%Y-%m-%d %H:%M:%S')

collection = "CRODEN_R16"

@app.route("/")
def helloFromBubble():
    return "Hey from Bubble!"

@app.route("/start_match")
def set_start_match():
    startToday = datetime.datetime.utcnow()
    file = open("set_start_match.txt", "w")
    file.write(str(startToday))
    file.close()
    return(json.dumps(startToday, default=str))

@app.route("/list-entities")
def list_entities():
    currDict = {}
    for i in entityDict:
        names = i.split("_")
        entity_name = names[0] + names[1]
        currDict[i] = {}
        currDict[i]["name"] = entity_name
        currDict[i]["image_url"] = entityDict[i][-1]
    return(json.dumps(currDict, default=str))


@socketio.on('entities')
def entity_tweets(entity_name):
    print(entity_name)
    file = open("set_start_match.txt", "r")
    for line in file:
        start_today = datetime.datetime.strptime(
            line.split('.')[0], '%Y-%m-%d %H:%M:%S')
    print(start_today)
    while True:
        now = datetime.datetime.utcnow()
        diff = now - start_today
        request_match = start_match + diff
        for post in db[collection].find():
        print(post)
            if "emotion" not in post.keys():
                continue
            if post["timeStamp"] > request_match:
                if post["entity_name"] == entity_name:
                    app.logger.info("Satisfied")
                    currDict = {}
                    currDict["entity"] = post["entity_name"]
                    currDict["emotion"] = max(
                        post["emotion"].items(), key=operator.itemgetter(1))[0]
                    currDict["profile_image"] = post["userProfile"]
                    currDict["tweet"] = post["tweet"].encode('utf-8')
                    currDict_json = json.dumps(currDict, default=str)
                    emit('Entity_Tweet', {'data':currDict_json})
                    del currDict
                    del currDict_json
                else:
            emit('Entity_Tweet', {'data':'ignored.'})
                    #print("Ignored.")
                    #app.logger.info("Ignored")
    return "Completed."

if(__name__ == "__main__"):
    socketio.run(app, host='0.0.0.0', debug=False)

此代码可以正常工作,但过了一段时间,它只会显示一条消息:

  

[2018-07-04 13:06:11 +0000] [3469] [严重]工作人员超时   (pid:3475)消息处理程序错误回溯(最近一次呼叫过去):
  文件“ /usr/local/lib/python2.7/dist-packages/engineio/server.py”,行   411,在_trigger_event中       返回self.handlersevent文件“ /usr/local/lib/python2.7/dist-packages/socketio/server.py”,第522行,   在_handle_eio_message中       self._handle_event(sid,pkt.namespace,pkt.id,pkt.data)文件“ /usr/local/lib/python2.7/dist-packages/socketio/server.py”,第458行,   在_handle_event中       self._handle_event_internal(self,sid,data,namespace,id)文件“ /usr/local/lib/python2.7/dist-packages/socketio/server.py”,第461行,   在_handle_event_internal       r = server._trigger_event(数据[0],名称空间,sid,*数据[1:])文件“ /usr/local/lib/python2.7/dist-packages/socketio/server.py”,行   490,在_trigger_event中       返回self.handlers [namespace]事件文件“ /usr/local/lib/python2.7/dist-packages/flask_socketio/init.py”,   _handler中的第251行       * args)文件“ /usr/local/lib/python2.7/dist-packages/flask_socketio/init.py”,   _handle_event中的第634行       ret = handler(* args)在entity_tweets中,文件“ /home/aviral/dev/bubble-api/index.py”,第80行       打印(发布)文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py”,   第196行,在handle_abort中       sys.exit(1)SystemExit:1 [2018-07-04 13:06:42 +0000] [3475] [INFO]工人退出(pid:3475)[2018-07-04 13:06:42 +0000] [3487]   [INFO]使用pid引导工人:3487 [2018-07-04 13:07:55 +0000]   [3487] [ERROR]套接字错误处理请求。追溯(最新   最后调用):文件   “ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   66号线,在手柄中       six.reraise(* sys.exc_info())文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   56号线,在手柄中       self.handle_request(listener_name,req,client,addr)文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   第129行,在handle_request中       six.reraise(* sys.exc_info())文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   第107行,在handle_request中       respiter = self.wsgi(environ,resp.start_response)文件“ /usr/local/lib/python2.7/dist-packages/flask/app.py”,第2309行,在   致电       返回self.wsgi_app(环境,start_response)文件“ /usr/local/lib/python2.7/dist-packages/flask_socketio/init.py”,   致电中的第43行       start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/middleware.py”,行   47,在致电中       返回self.engineio_app.handle_request(environ,start_response)文件“ /usr/local/lib/python2.7/dist-packages/socketio/server.py”,行   360,在handle_request中       返回self.eio.handle_request(environ,start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/server.py”,第275行,   在handle_request中       环境,start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/socket.py”,第91行,   在handle_get_request中       start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/socket.py”,第133行,   在_upgrade_websocket中       返回ws(环境,start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/async_eventlet.py”,   第19行,致电       返回超级(WebSocketWSGI,自身)。调用(环境,start_response)文件   “ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   129,在致电中       self.handler(ws)文件“ /usr/local/lib/python2.7/dist-packages/engineio/socket.py”,第158行,   在_websocket_handler中       pkt = ws.wait()文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   787,等待中       为我在self.iterator中:文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   642,在_iter_frames中       message = self._recv_frame(message = fragmented_message)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   668,在_recv_frame中       标头= recv(2)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   577,以_get_bytes为单位       d = self.socket.recv(数字字节-len(数据))文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   RECV中的363行       返回self._recv_loop(self.fd.recv,b'',bufsize,标志)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   _recv_loop中的第357行       self._read_trampoline()文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   _read_trampoline中的第328行       timeout_exc = socket_timeout('timed out'))文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   _trampoline中的第207行       mark_as_closed = self._mark_as_closed)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/hubs/init.py”,   蹦床上的163号线       返回hub.switch()文件“ /usr/local/lib/python2.7/dist-packages/eventlet/hubs/hub.py”,行   295,在开关中       返回self.greenlet.switch()超时:超时[2018-07-04 13:08:58 +0000] [3487] [ERROR]套接字错误处理请求。   追溯(最近一次通话):文件   “ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   66号线,在手柄中       six.reraise(* sys.exc_info())文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   56号线,在手柄中       self.handle_request(listener_name,req,client,addr)文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   第129行,在handle_request中       six.reraise(* sys.exc_info())文件“ /usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py”,   第107行,在handle_request中       respiter = self.wsgi(environ,resp.start_response)文件“ /usr/local/lib/python2.7/dist-packages/flask/app.py”,第2309行,在   致电       返回self.wsgi_app(环境,start_response)文件“ /usr/local/lib/python2.7/dist-packages/flask_socketio/init.py”,   致电中的第43行       start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/middleware.py”,行   47,在致电中       返回self.engineio_app.handle_request(environ,start_response)文件“ /usr/local/lib/python2.7/dist-packages/socketio/server.py”,行   360,在handle_request中       返回self.eio.handle_request(environ,start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/server.py”,第275行,   在handle_request中       环境,start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/socket.py”,第91行,   在handle_get_request中       start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/socket.py”,第133行,   在_upgrade_websocket中       返回ws(环境,start_response)文件“ /usr/local/lib/python2.7/dist-packages/engineio/async_eventlet.py”,   第19行,致电       返回超级(WebSocketWSGI,自身)。调用(环境,start_response)文件   “ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   129,在致电中       self.handler(ws)文件“ /usr/local/lib/python2.7/dist-packages/engineio/socket.py”,第158行,   在_websocket_handler中       pkt = ws.wait()文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   787,等待中       为我在self.iterator中:文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   642,在_iter_frames中       message = self._recv_frame(message = fragmented_message)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   668,在_recv_frame中       标头= recv(2)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/websocket.py”,行   577,以_get_bytes为单位       d = self.socket.recv(数字字节-len(数据))文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   RECV中的363行       返回self._recv_loop(self.fd.recv,b'',bufsize,标志)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   _recv_loop中的第357行       self._read_trampoline()文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   _read_trampoline中的第328行       timeout_exc = socket_timeout('timed out'))文件“ /usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py”,   _trampoline中的第207行       mark_as_closed = self._mark_as_closed)文件“ /usr/local/lib/python2.7/dist-packages/eventlet/hubs/init.py”,   蹦床上的163号线       返回hub.switch()文件“ /usr/local/lib/python2.7/dist-packages/eventlet/hubs/hub.py”,行   295,在开关中       返回self.greenlet.switch()超时:超时

我通过以下方式运行该应用程序:

gunicorn --worker-class eventlet -w 1 --bind 0.0.0.0:5000 index:app

1 个答案:

答案 0 :(得分:2)

while True:永久性地阻止了工作进程,因此工作进程达到超时并失败。尝试避免端点(Flask或SocketIO)方法中的这种无限循环。