关于python:我应该在相同的端口上运行普通的烧瓶app和套接字吗?

Should I run normal flask app and sockets on the same ports?

我正在通过Gunicorn、Eventlet和nginx运行一个模块index.py作为flask应用程序。

代码工作正常,但我无法使其在所需时间内运行。

这是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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] [CRITICAL] WORKER TIMEOUT
(pid:3475) message handler error Traceback (most recent call last):
File"/usr/local/lib/python2.7/dist-packages/engineio/server.py", line
411, in _trigger_event
return self.handlersevent File"/usr/local/lib/python2.7/dist-packages/socketio/server.py", line 522,
in _handle_eio_message
self._handle_event(sid, pkt.namespace, pkt.id, pkt.data) File"/usr/local/lib/python2.7/dist-packages/socketio/server.py", line 458,
in _handle_event
self._handle_event_internal(self, sid, data, namespace, id) File"/usr/local/lib/python2.7/dist-packages/socketio/server.py", line 461,
in _handle_event_internal
r = server._trigger_event(data[0], namespace, sid, *data[1:]) File"/usr/local/lib/python2.7/dist-packages/socketio/server.py", line
490, in _trigger_event
return self.handlers[namespace]event File"/usr/local/lib/python2.7/dist-packages/flask_socketio/init.py",
line 251, in _handler
*args) File"/usr/local/lib/python2.7/dist-packages/flask_socketio/init.py",
line 634, in _handle_event
ret = handler(*args) File"/home/aviral/dev/bubble-api/index.py", line 80, in entity_tweets
print(post) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py",
line 196, in handle_abort
sys.exit(1) SystemExit: 1 [2018-07-04 13:06:42 +0000] [3475] [INFO] Worker exiting (pid: 3475) [2018-07-04 13:06:42 +0000] [3487]
[INFO] Booting worker with pid: 3487 [2018-07-04 13:07:55 +0000]
[3487] [ERROR] Socket error processing request. Traceback (most recent
call last): File
"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 66, in handle
six.reraise(*sys.exc_info()) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 56, in handle
self.handle_request(listener_name, req, client, addr) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 129, in handle_request
six.reraise(*sys.exc_info()) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 107, in handle_request
respiter = self.wsgi(environ, resp.start_response) File"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in
call
return self.wsgi_app(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/flask_socketio/init.py",
line 43, in call
start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/middleware.py", line
47, in call
return self.engineio_app.handle_request(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/socketio/server.py", line
360, in handle_request
return self.eio.handle_request(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/server.py", line 275,
in handle_request
environ, start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/socket.py", line 91,
in handle_get_request
start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/socket.py", line 133,
in _upgrade_websocket
return ws(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/async_eventlet.py",
line 19, in call
return super(WebSocketWSGI, self).call(environ, start_response) File
"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
129, in call
self.handler(ws) File"/usr/local/lib/python2.7/dist-packages/engineio/socket.py", line 158,
in _websocket_handler
pkt = ws.wait() File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
787, in wait
for i in self.iterator: File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
642, in _iter_frames
message = self._recv_frame(message=fragmented_message) File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
668, in _recv_frame
header = recv(2) File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
577, in _get_bytes
d = self.socket.recv(numbytes - len(data)) File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 363, in recv
return self._recv_loop(self.fd.recv, b'', bufsize, flags) File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 357, in _recv_loop
self._read_trampoline() File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 328, in _read_trampoline
timeout_exc=socket_timeout('timed out')) File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 207, in _trampoline
mark_as_closed=self._mark_as_closed) File"/usr/local/lib/python2.7/dist-packages/eventlet/hubs/init.py",
line 163, in trampoline
return hub.switch() File"/usr/local/lib/python2.7/dist-packages/eventlet/hubs/hub.py", line
295, in switch
return self.greenlet.switch() timeout: timed out [2018-07-04 13:08:58 +0000] [3487] [ERROR] Socket error processing request.
Traceback (most recent call last): File
"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 66, in handle
six.reraise(*sys.exc_info()) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 56, in handle
self.handle_request(listener_name, req, client, addr) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 129, in handle_request
six.reraise(*sys.exc_info()) File"/usr/local/lib/python2.7/dist-packages/gunicorn/workers/async.py",
line 107, in handle_request
respiter = self.wsgi(environ, resp.start_response) File"/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in
call
return self.wsgi_app(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/flask_socketio/init.py",
line 43, in call
start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/middleware.py", line
47, in call
return self.engineio_app.handle_request(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/socketio/server.py", line
360, in handle_request
return self.eio.handle_request(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/server.py", line 275,
in handle_request
environ, start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/socket.py", line 91,
in handle_get_request
start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/socket.py", line 133,
in _upgrade_websocket
return ws(environ, start_response) File"/usr/local/lib/python2.7/dist-packages/engineio/async_eventlet.py",
line 19, in call
return super(WebSocketWSGI, self).call(environ, start_response) File
"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
129, in call
self.handler(ws) File"/usr/local/lib/python2.7/dist-packages/engineio/socket.py", line 158,
in _websocket_handler
pkt = ws.wait() File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
787, in wait
for i in self.iterator: File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
642, in _iter_frames
message = self._recv_frame(message=fragmented_message) File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
668, in _recv_frame
header = recv(2) File"/usr/local/lib/python2.7/dist-packages/eventlet/websocket.py", line
577, in _get_bytes
d = self.socket.recv(numbytes - len(data)) File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 363, in recv
return self._recv_loop(self.fd.recv, b'', bufsize, flags) File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 357, in _recv_loop
self._read_trampoline() File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 328, in _read_trampoline
timeout_exc=socket_timeout('timed out')) File"/usr/local/lib/python2.7/dist-packages/eventlet/greenio/base.py",
line 207, in _trampoline
mark_as_closed=self._mark_as_closed) File"/usr/local/lib/python2.7/dist-packages/eventlet/hubs/init.py",
line 163, in trampoline
return hub.switch() File"/usr/local/lib/python2.7/dist-packages/eventlet/hubs/hub.py", line
295, in switch
return self.greenlet.switch() timeout: timed out

我运行应用程序时使用:

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


while True:永远阻塞了工人,所以工人到达超时状态,失败了。尽量避免在端点(烧瓶或插座)方法中出现这种无限循环。