【问题标题】:Using ZeroMQ in a PHP script inside Apache在 Apache 内的 PHP 脚本中使用 ZeroMQ
【发布时间】:2012-08-25 13:40:54
【问题描述】:

我想使用 ZeroMQ 发布者/订阅者将数据从我的 Web 应用程序分派到多个服务器。

我使用 Apache 和 PHP 作为 web 应用程序,我的 php 脚本的工作方式如下:

//Initialization
$context = new ZMQContext();
$publisher = $context->getSocket(ZMQ::SOCKET_PUB);
$publisher->bind("tcp://*:5556");

//Then publishing for testing:

$publisher->send("test");
$publisher->send("test");
$publisher->send("test");
$publisher->send("test");
$publisher->send("test");

为了测试,我从 python 的文档中改编了一个订阅者:

import sys
import zmq

#  Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

socket.connect ("tcp://localhost:5556")

# Subscribe to zipcode, default is NYC, 10001
socket.setsockopt(zmq.SUBSCRIBE, "")

print "Waiting..."
# Process 5 updates
for update_nbr in range (5):
    string = socket.recv()
    print string

当我从命令行运行 php 脚本但不能通过 Apache 运行时(当脚本通过 Web 浏览器运行时),整个事情都有效。

我应该对我的 Apache 配置做些什么来使其正常工作?

谢谢

亚历山大

【问题讨论】:

    标签: php apache zeromq


    【解决方案1】:

    似乎唯一的问题是连接没有时间建立。

    在绑定之后和发送之前在发布者上添加休眠确实解决了问题,即使不是很优雅。

    这里解释了这个问题:

    http://zguide.zeromq.org/page:all#Getting-the-Message-Out

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2021-04-01
      • 2019-05-14
      • 2014-09-07
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多