在配置了Apache服务器之后,为什么zerorpc不能在我的Flask应用程序中工作?

2024-03-29 04:50:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个Flask应用程序,它使用zerorpc在进程之间发送和接收数据,当我单独测试它时,它运行良好。不过,在使用Apache进行配置之后,我在尝试进行RPC调用时遇到了这个错误(其他不使用zerorpc的端点)我正在CentOS 7机器上开发

[Tue Nov 19 21:26:37.222041 2019] [wsgi:error] [pid 31181] [client 128.84.xxx.xxx:60822] greenlet.error: cannot switch to a different thread, referer: http://ec2-18-xxx-xxx-199.us-east-2.compute.amazonaws.com/login?username=myname&password=Password123%21%21%21&ip=1.1.1.1&result=true&header_json=lkwejlekwjf

我试着按照其他帖子的建议添加对monkey.patch\u all()的调用,但没有解决这个问题

客户:

from flask import Flask, request
from classes import EphemeralLoginData
import pickle
import zerorpc
import os

#####################################################
app = Flask(__name__)
# ZeroRPC client global variable
db_c = zerorpc.Client(heartbeat=None)
# Start up the ZeroRPC client
db_socket_path = "/tmp/db_sock__"
db_c.connect("ipc://{}".format(db_socket_path))
#####################################################

@app.route("/login/", methods=["POST", "GET"])
def login():
   print("Request to login/ received")
   login_data = EphemeralLoginData(request.args["username"], request.args["password"], request.args["ip"], request.args["result"], request.args["header_json"], datetime.now())

   # Now call store in the database accessor file
   print("Sending to database accessor to be stored..")
   db_c.store_ephemeral_entry(pickle.dumps(login_data))

服务器:

import pickle
import zerorpc

class DatabaseRPC(object):
   @zerorpc.stream
   def store_ephemeral_entry(self, serialized_login_data):
       login_data = pickle.loads(serialized_login_data)
       print("Received request to store login data")
       return "Successfully added ephemeral entry"

s = zerorpc.Server(DatabaseRPC(), heartbeat=None)
db_socket_path = "/tmp/db_sock__"
s.bind("ipc://{}".format(db_socket_path))
print("Ready")

zerorpc.gevent.spawn(s.run) # previously this was just s.run() without the while loop below, but I changed it to this based on other stack overflow posts I saw. Neither fixes the issue for me.
while True:
    zerorpc.gevent.sleep(10)

谢谢


Tags: thetopathstoreimportdbdatarequest