ZMQ getsocket 方法 [英] ZMQ getsocket method

查看:30
本文介绍了ZMQ getsocket 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

zeromq 中 getSocket(type,persistence_id,callback) 的目的是什么?

What is the purpose of getSocket(type,persistence_id,callback) in zeromq?

如果上下文中不存在具有相同persistence_id的套接字,它会创建一个新套接字吗?

Will it create a new socket if one doesn't exist with the same persistence_id in the context?

这是我的客户

function newSocket(ZMQSocket $soc, $pid)    {
    echo $pid;
}

$context = new ZMQContext();

$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'mysocket', 'newSocket');

$socket->setSockOpt(ZMQ::SOCKOPT_HWM,5);

$socket->connect("tcp://172.16.136.59:5555");
for($i=0;$i<10;$i++)
{
    var_dump($socket->send("hai",ZMQ::MODE_NOBLOCK));
    sleep(2);
}

我同时运行这个客户端多少次[第 n-1 个客户端启动后的第 n 个客户端],回调正在执行.这是理想的行为吗?什么情况下socket结构会被重用?

How many ever times I run this client simultaneously[nth client after n-1th client started], the callback is getting executed. Is this the desired behavior? What are all the situations, where the socket structure will be reused?

推荐答案

持久性的作用是通知内存分配器使用持久性内存分配函数,它将以某种方式分配上下文(如果你问,任何套接字)它不会在请求结束时消失,但会持续 PHP 进程的生命周期.如果有意义的话,它的工作方式与某些连接池库相同.回调将在第一次创建套接字时使用.持久化需要在上下文中设置,例如:

What persistance does is signal the memory allocator to use the persistent memory allocation functions, which will alloc the context (and if you ask, any sockets) in a way that wont go away at the end of the request, but will last the lifetime of a PHP process. It sort of works the same way as some of the connection pooling libs, if that makes sense. The callback will be used on the first time the socket is created. Persistance needs to be set in the context, for example:

<?php

function newSocket(ZMQSocket $soc, $pid)    {
    echo "Creating New Socket \n", $pid, "\n";
}

echo "=========Creating without persistence ==========\n";

$context = new ZMQContext(1, false);

$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'mysocket', 'newSocket');

$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'mysocket', 'newSocket');

echo "========Creating with persistence ==========\n";

$context = new ZMQContext(1, true);

$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'mysocket', 'newSocket');

$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'mysocket', 'newSocket');

会给你

# php -dextension=zmq.so test.php 
=========Creating without persistence ==========
Creating New Socket 
mysocket
Creating New Socket 
mysocket
========Creating with persistence ==========
Creating New Socket 
mysocket

在第一个实例中,如果持久化为 false,则每次都会重新创建套接字,并触发回调.第二种,在持久化的情况下,只返回套接字,不触发回调.

In the first instance, with false to persistance, the socket is recreated each time, and the callback fired. In the second, with persistance on, the socket is just returned, and the callback not triggered.

这篇关于ZMQ getsocket 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆