Python Azure:ssl.SSLEOFError:EOF 发生违反协议 (_ssl.c:2427)

问题描述 投票:0回答:1

我有以下 Flask 应用程序代码:

def do_binance_trading(queue): 
    print("Going to start a socket. . .")
    socket_manager = BinanceSocketManager.getInstance() 
    socket_manager.startSocket(queue) 
    print("Socket started. . .")

@app.route("/") 
def main(): 
global process
global queue
global latestQueueData print("Ping app. . .")
if process is not None and process.is_alive():
    print("App is running. Trading process id:" + str(process.pid))    
    # Retrieve all data from the queue
    queue_data = []
    while not queue.empty():
        queue_data.append(queue.get())

    if len(queue_data) > 0 and latestQueueData != queue_data[-1]:
        latestQueueData = queue_data[-1]

    # Display all data in the queue
    return latestQueueData.replace("\n", "<br>"), 200
else:
    print("Starting App. . .")
    queue = multiprocessing.Queue()
    process = multiprocessing.Process(target=do_binance_trading, args=(queue,))
    process.start()
    print("Process started. . .")
    return "Trading started", 200

if name == "main": 
    app.run()

应用程序部署到 Azure Web 服务。逻辑非常简单:

  1. 运行应用程序。流程开始
  2. 根刷新时 - 从队列中获取进程的统计信息。

应用程序总体工作正常,但有时,在没有任何影响或逻辑的情况下,进程会终止,我得到以下 Azure 日志跟踪:

2024-04-27T09:59:19.988977259Z Process Process-14:
2024-04-27T09:59:19.990693175Z Traceback (most recent call last):
2024-04-27T09:59:19.991752385Z   File "/opt/python/3.11.7/lib/python3.11/multiprocessing/process.py", line 314, in \_bootstrap
2024-04-27T09:59:19.991789786Z     self.run()
2024-04-27T09:59:19.991796086Z   File "/opt/python/3.11.7/lib/python3.11/multiprocessing/process.py", line 108, in run
2024-04-27T09:59:19.991800386Z     self.\_target(\*self.\_args, \*\*self.\_kwargs)
2024-04-27T09:59:19.991824686Z   File "/tmp/8dc60406808a4da/app.py", line 29, in do_binance_trading
2024-04-27T09:59:19.991829486Z     socket_manager.startSocket(queue)
2024-04-27T09:59:19.991832986Z   File "/tmp/8dc60406808a4da/data/binance_data_socket.py", line 73, in startSocket
2024-04-27T09:59:19.991837086Z     rel.dispatch()
2024-04-27T09:59:19.991840786Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/rel.py", line 228, in dispatch
2024-04-27T09:59:19.991844886Z     registrar.dispatch()
2024-04-27T09:59:19.991849086Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/registrar.py", line 126, in dispatch
2024-04-27T09:59:19.991852986Z     if not self.loop():
2024-04-27T09:59:19.991857186Z            ^^^^^^^^^^^
2024-04-27T09:59:19.991861086Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/registrar.py", line 135, in loop
2024-04-27T09:59:19.991864786Z     e = self.check_events()
2024-04-27T09:59:19.991868786Z         ^^^^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.991872386Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/registrar.py", line 300, in check_events
2024-04-27T09:59:19.991876686Z     self.handle_error(fd)
2024-04-27T09:59:19.991880286Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/registrar.py", line 185, in handle_error
2024-04-27T09:59:19.991884187Z     self.callback('read', fd)
2024-04-27T09:59:19.991888087Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/registrar.py", line 179, in callback
2024-04-27T09:59:19.991898687Z     self.events\[etype\]\[fd\].callback()
2024-04-27T09:59:19.991902887Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/rel/listener.py", line 127, in callback
2024-04-27T09:59:19.991907287Z     if not self.cb(\*self.args) and not self.persist and self.active:
2024-04-27T09:59:19.991924987Z            ^^^^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.991929087Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_app.py", line 515, in read
2024-04-27T09:59:19.991933287Z     op_code, frame = self.sock.recv_data_frame(True)
2024-04-27T09:59:19.991936687Z                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.991940587Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_core.py", line 461, in recv_data_frame
2024-04-27T09:59:19.991944587Z     self.pong(frame.data)
2024-04-27T09:59:19.991948187Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_core.py", line 377, in pong
2024-04-27T09:59:19.991952087Z     self.send(payload, ABNF.OPCODE_PONG)
2024-04-27T09:59:19.991957087Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_core.py", line 297, in send
2024-04-27T09:59:19.991960987Z     return self.send_frame(frame)
2024-04-27T09:59:19.991964487Z            ^^^^^^^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.991967987Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_core.py", line 337, in send_frame
2024-04-27T09:59:19.991971787Z     l = self.\_send(data)
2024-04-27T09:59:19.991975087Z         ^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.991978787Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_core.py", line 559, in \_send
2024-04-27T09:59:19.992036988Z     return send(self.sock, data)
2024-04-27T09:59:19.992043088Z            ^^^^^^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.992047388Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_socket.py", line 176, in send
2024-04-27T09:59:19.992051988Z     return \_send()
2024-04-27T09:59:19.992056188Z            ^^^^^^^
2024-04-27T09:59:19.992060688Z   File "/tmp/8dc60406808a4da/antenv/lib/python3.11/site-packages/websocket/\_socket.py", line 153, in \_send
2024-04-27T09:59:19.992065088Z     return sock.send(data)
2024-04-27T09:59:19.992069088Z            ^^^^^^^^^^^^^^^
2024-04-27T09:59:19.992073088Z   File "/opt/python/3.11.7/lib/python3.11/ssl.py", line 1243, in send
2024-04-27T09:59:19.998976055Z     return self.\_sslobj.write(data)
2024-04-27T09:59:19.998993255Z            ^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-27T09:59:19.998998955Z ssl.SSLEOFError: EOF occurred in violation of protocol (\_ssl.c:2427)

进程重新启动,应用程序继续运行,直到出现另一个异常。我有一种感觉,我缺少 Azure 中的某些配置。应用程序配置为“始终开启”

完整的应用程序配置: 在此输入图片描述

尝试了我可以在谷歌上搜索到的关于这个主题的所有内容,但没有运气

python azure-web-app-service
1个回答
0
投票

ssl.SSLEOFError:EOF 发生违反协议 (_ssl.c:2427)

为了避免上述错误消息,

确保您使用的是最新版本的库。

确保代码或 Azure 环境中没有可能过早关闭连接的超时设置。

我成功地将一个简单的 Flask 应用程序部署到 Azure 应用服务,没有任何问题。

应用程序.py

from flask import Flask
import multiprocessing
app = Flask(__name__)
process = None
queue = multiprocessing.Queue()
latestQueueData = ""
def do_binance_trading(queue): 
    print("Going to start a socket. . .")
    print("Socket started. . .")
@app.route("/") 
def main(): 
    global process
    global queue
    global latestQueueData 
    print("Ping app. . .")
    if process is not None and process.is_alive():
        print("App is running. Trading process id:" + str(process.pid))           
        queue_data = []
        while not queue.empty():
            queue_data.append(queue.get())
        if len(queue_data) > 0 and latestQueueData != queue_data[-1]:
            latestQueueData = queue_data[-1]   
        return latestQueueData.replace("\n", "<br>"), 200
    else:
        print("Starting App. . .")
        queue = multiprocessing.Queue()
        process = multiprocessing.Process(target=do_binance_trading, args=(queue,))
        process.start()
        print("Process started. . .")
        return "Trading started", 200
if __name__ == "__main__": 
    app.run()

本地输出:

enter image description here

我通过 Visual Studio 代码扩展部署了应用程序

enter image description here

enter image description here

部署后输出:

enter image description here

日志流:

enter image description here

应用程序配置: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.