Skip to main content
 首页 » 编程设计

python之Flask 应用程序中的 zerorpc 在 apache 服务器中启动时不工作,但在 pycharm 启动时工作正常

2024年11月01日27lori

<分区>

我正在使用 flask 应用程序来显示使用零 rpc 订阅者读取的数据。 在开发过程中,当我使用 Pycharm 启动应用程序时,它运行良好,但是一旦部署在 Apache 服务器中,订阅者就无法获取任何数据。基本上,即使发布者正在发布数据,订阅者中的方法也不会被调用。

谢谢

编辑:(添加更多信息):

在 app_start.py 中

from gevent import monkey 
monkey.patch_all() 
from flask import Flask 
app = Flask(__name__) 
 
import stratgy_subscriber 
startegy_state_info_provider = stratgy_subscriber.StartegyStateInfoProvider() 
@app.route('/') 
def strategy_info(): 
    return startegy_state_info_provider.ip_port_to_subscriber_map[('0.0.0.0', '4249')] 
 
if __name__ == '__main__': 
    startegy_state_info_provider.start_subscriber('0.0.0.0', '4249') 
    app.run() 

我正在使用以下代码连接到订阅者。 在 stratgy_subscriber.py

from gevent import monkey 
monkey.patch_all() 
 
class StartegyStateInfoProvider(object): 
 
  def __init__(self):   
      self.ip_port_to_subscriber_map = {} 
 
  def start_subscriber(machine_ip, port): 
      strategy_state_subscriber = StrategyStateSubscriber() 
      ip_port_to_subscriber_map[(machine_ip, port)] = strategy_state_subscriber 
      end_point = 'tcp://' + machine_ip + ':' + port 
      strategy_state_subscriber = zerorpc.Subscriber(strategy_state_subscriber) 
      strategy_state_subscriber.connect(end_point) 
      gevent.spawn(strategy_state_subscriber.run) 
 
class StrategyStateSubscriber(): 
  def __init__(self): 
    self.strategy_id_to_info_map = {} 
  def update_strategy_state(self, strategy_id, updated_strategy_state):  
    from datetime import datetime 
    current_time = datetime.strftime(datetime.now(), '%H:%M:%S') 
    updated_strategy_state.append(current_time) 
    print updated_strategy_state 
    strategy_id_to_info_map[strategy_id] = updated_strategy_state 

这是发布者`

endpoint = "tcp://0.0.0.0:4249" 
publisher = zerorpc.Publisher() 
publisher.bind(endpoint) 
 
for a in xrange(0, 1000): 
    info = [False, a * a, 3 * a, a % 20, a % 50] 
    publisher.update_strategy_state(a, info) 
    if a < 50: 
        gevent.sleep(1) 
    elif a >= 50: 
        gevent.sleep(2) 

订阅后,即 gevent.spawn(strategy_state_subscriber.run),update_strategy_state 应该在每次发布者发布未发生的数据时被调用。

我遇到以下异常:

[Wed May 28 11:24:03 2014] [error] Traceback (most recent call last): 
[Wed May 28 11:24:03 2014] [error]   File "/home/sricharan/git_ceres_viewer/src/strategy_state_subscription.py", line 68, in subscribe 
[Wed May 28 11:24:03 2014] [error]     subscriber = zerorpc.Subscriber(service) 
[Wed May 28 11:24:03 2014] [error]   File "/usr/local/lib/python2.7/dist-packages/zerorpc/core.py", line 371, in __init__ 
[Wed May 28 11:24:03 2014] [error]     zmq_socket=zmq.SUB) 
[Wed May 28 11:24:03 2014] [error]   File "/usr/local/lib/python2.7/dist-packages/zerorpc/core.py", line 312, in __init__ 
[Wed May 28 11:24:03 2014] [error]     super(Puller, self).__init__(zmq_socket, context=context) 
[Wed May 28 11:24:03 2014] [error]   File "/usr/local/lib/python2.7/dist-packages/zerorpc/socket.py", line 34, in __init__ 
[Wed May 28 11:24:03 2014] [error]     self._events = Events(zmq_socket_type, context) 
[Wed May 28 11:24:03 2014] [error]   File "/usr/local/lib/python2.7/dist-packages/zerorpc/events.py", line 177, in __init__ 
[Wed May 28 11:24:03 2014] [error]     self._socket = zmq.Socket(self._context, zmq_socket_type) 
[Wed May 28 11:24:03 2014] [error]   File "/usr/local/lib/python2.7/dist-packages/zerorpc/gevent_zmq.py", line 61, in __init__ 
[Wed May 28 11:24:03 2014] [error]     self.__dict__["_state_event"] = gevent.hub.get_hub().loop.io( 
[Wed May 28 11:24:03 2014] [error]   File "/usr/lib/python2.7/dist-packages/gevent/hub.py", line 135, in get_hub 
[Wed May 28 11:24:03 2014] [error]     raise NotImplementedError('gevent is only usable from a single thread') 
[Wed May 28 11:24:03 2014] [error] NotImplementedError: gevent is only usable from a single thread 

经过一些研究,我发现:- gevent 与 Apache 的多线程工作模型不兼容