0MQ是会阻塞的,不要字面上看到队列就等同非阻塞。
如果你是希望通过0MQ来做缓冲队列,非阻塞的效果,那你就必须清楚 0MQ Socket是会阻塞,你要搞清楚0MQ Socket与队列的关系。
官方协议文档规定了,一部分类型的 0MQ Socket为不阻塞发送,而另一部分类型则是阻塞发送的。不阻塞发送同时往往也意味着丢弃消息,相反不能丢弃消息的则要求阻塞发送。这里的阻塞发送,不是说像tcp那样确保送到目的地,而是必须有目的地可以发送。而队列的作用就是架在发送端和目的地之间作缓冲。如果连目的地也没有,那么队列也就失去存在的意义了,所以只好将发送阻塞起来。到这里你可能会撇嘴,不是发送端缓冲在队列,在接收端建立起连接后,接收端从队列中取消息。这是你相像出来的 0MQ,而非人家设计出来 0MQ。还是要看人家的(协议设计)文档。
各种类型的详细行为责任定义:
- http://rfc.zeromq.org/spec:28/REQREP defines the semantics of REQ, REP, DEALER and ROUTER sockets.
- http://rfc.zeromq.org/spec:29/PUBSUB defines the semantics of PUB, XPUB, SUB and XSUB sockets.
- http://rfc.zeromq.org/spec:30/PIPELINE defines the semantics of PUSH and PULL sockets.
- http://rfc.zeromq.org/spec:31/EXPAIR defines the semantics of exclusive PAIR sockets.
The PAIR Socket Type
- SHALL create a double queue when initiating an outgoing connection to a peer, and SHALL maintain the double queue whether or not the connection is established.
- SHALL create a double queue when a peer connects to it. If this peer disconnects, the PAIR socket SHALL destroy its double queue and SHALL discard any messages it contains.
The PULL Socket Type
- SHALL create this queue when initiating an outgoing connection to a peer, and SHALL maintain the queue whether or not the connection is established.
- SHALL create this queue when a peer connects to it. If this peer disconnects, the PULL socket SHALL destroy its queue and SHALL discard any messages it contains.
The PUSH Socket Type
- SHALL create this queue when initiating an outgoing connection to a peer, and SHALL maintain the queue whether or not the connection is established.
- SHALL create this queue when a peer connects to it. If this peer disconnects, the PUSH socket SHALL destroy its queue and SHALL discard any messages it contains.
从上面一般行为规定可以看到,队列是与底层socket连接对应的,当底层没有任何socket连接时,队列就不存在。所以发送行为就有下面这样的规定。
The PAIR Socket Type
- SHALL consider its peer as available only when it has a outgoing queue that is not full.
- SHALL block on sending, or return a suitable error, when it has no available peer.
- SHALL not accept further messages when it has no available peer.
- SHALL NOT discard messages that it cannot queue.
The PUSH Socket Type
- SHALL consider a peer as available only when it has a outgoing queue that is not full.
- SHALL route outgoing messages to available peers using a round-robin strategy.
- SHALL block on sending, or return a suitable error, when it has no available peers.
- SHALL not accept further messages when it has no available peers.
- SHALL NOT discard messages that it cannot queue.
The DEALER Socket Type
- SHALL consider a peer as available only when it has a outgoing queue that is not full.
- SHALL route outgoing messages to available peers using a round-robin strategy.
- SHALL block on sending, or return a suitable error, when it has no available peers.
- SHALL not accept further messages when it has no available peers.
- SHALL NOT discard messages that it cannot queue.
The REQ Socket Type
- SHALL prefix the outgoing message with an empty delimiter frame.
- SHALL route outgoing messages to connected peers using a round-robin strategy.
- SHALL block on sending, or return a suitable error, when it has no connected peers.
- SHALL NOT discard messages that it cannot send to a connected peer.
1. 红色字眼 block on,说明了 0MQ Socket 在发送过程有可以阻塞。
2. ”consider a peer as available only when it has a outgoing queue that is not full“, 特别要注意peer,首先要有连接,只要这个连接关联的发送队列不满也就可以视作可以发送。
3. "block on sending, or return a suitable error, when it has no connected/available peers", 当没有可以发送的peer,(意思是a. 有连接但发送队列满;b. 无任何连接,也就没有队列了。)要么阻塞发送,要么就返回错误给调用者,由调用者自己决定丢弃消息或其它方式处理。
4. "not accept further messages when it has no available peers",这里是指 0MQ Socket 层不接受 0MQ Socket使用者发送的消息,不是说底层连接不接收消息。
现在是否清楚使用 0MQ时,在什么情况下可能会被阻塞了。
0MQ是会阻塞的,不要字面上看到队列就等同非阻塞。的更多相关文章
- (原创)JAVA阻塞队列LinkedBlockingQueue 以及非阻塞队列ConcurrentLinkedQueue 的区别
阻塞队列:线程安全 按 FIFO(先进先出)排序元素.队列的头部 是在队列中时间最长的元素.队列的尾部 是在队列中时间最短的元素.新元素插入到队列的尾部,并且队列检索操作会获得位于队列头部的元素.链接 ...
- Linux非阻塞IO(八)使用epoll重新实现非阻塞的回射服务器
本文无太多内容,主要是几个前面提到过的注意点: 一是epoll的fd需要重新装填.我们将tcp_connection_t的指针保存在数组中,所以我们以这个数组为依据,重新装填fd的监听事件. //重新 ...
- 008. 阻塞&非阻塞、同步&异步
阻塞 非阻塞:关注的对象是调用者: 阻塞:调用者发起调用后,处于等待状态,直到该调用有返回: 非阻塞:调用者发起调用后,不需要等待返回,可以往下执行: 同步 异步: 关注的对象是被调用者: 同步:服 ...
- IO模型浅析-阻塞、非阻塞、IO复用、信号驱动、异步IO、同步IO
最近看到OVS用户态的代码,在接收内核态信息的时候,使用了Epoll多路复用机制,对其十分不解,于是从网上找了一些资料,学习了一下<UNIX网络变成卷1:套接字联网API>这本书对应的章节 ...
- EAGAIN、EWOULDBLOCK、EINTR与非阻塞 长连接
EAGAIN.EWOULDBLOCK.EINTR与非阻塞 长连接 EWOULDBLOCK用于非阻塞模式,不需要重新读或者写 EINTR指操作被中断唤醒,需要重新读/写 在Linux环境下开发经常会碰到 ...
- 阻塞I/O、非阻塞I/O和I/O多路复用、怎样理解阻塞非阻塞与同步异步的区别?
“阻塞”与"非阻塞"与"同步"与“异步"不能简单的从字面理解,提供一个从分布式系统角度的回答.1.同步与异步 同步和异步关注的是消息通信机制 (syn ...
- 简述linux同步与异步、阻塞与非阻塞概念以及五种IO模型
1.概念剖析 相信很多从事linux后台开发工作的都接触过同步&异步.阻塞&非阻塞这样的概念,也相信都曾经产生过误解,比如认为同步就是阻塞.异步就是非阻塞,下面我们先剖析下这几个概念分 ...
- socket阻塞与非阻塞,同步与异步、I/O模型,select与poll、epoll比较
1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端: 同步: 所谓同步,就 ...
- socket阻塞与非阻塞,同步与异步
socket阻塞与非阻塞,同步与异步 作者:huangguisu 转自:http://blog.csdn.net/hguisu/article/details/7453390 1. 概念理解 在进行网 ...
随机推荐
- nginx::配置https/反向代理
vim /etc/nginx/nginx.conf user nginx; worker_processes ; error_log /var/log/nginx/error.log warn; pi ...
- Leetcode(2)两数相加
Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两 ...
- git 的一些指令
作为一个前端,git的各种指令应该是需要掌握的,因为前一段时间遇到了一些问题,总觉得每次遇到点问题都去百度这样总归不大好,还是记录一下比较常见的一些指令吧.以后遇到新的再继续添加. 1. 新建本地分支 ...
- vue路由安装
1.安装路由: vue ui cnpm install vue-router 2.使用,导入: 默认创建项目的时候就已经帮你写好了. import router from "vue-rout ...
- MongoDB分页查询优化方法
在网上看到很多关于MongoDB分页查询优化的文章,如出一辙.笔者自己实际生产中也遇到此问题,所以看了很多篇文章,这里分享一篇简明扼要的文章分享给大家,希望对大家在使用MongoDB时有所帮助. 凡事 ...
- AI Conference 2018人工智能大会 参会总结
主 题:AI Conference 2018人工智能大会 时 间:2018.04.11-4.13 地 点:北京国际饭店会议中心 发起人:O'Reilly 和 Intel 参与部门:研发设计部 参会人员 ...
- kaldi中CD-DNN-HMM网络参数更新公式手写推导
在基于DNN-HMM的语音识别中,DNN的作用跟GMM是一样的,即它是取代GMM的,具体作用是算特征值对每个三音素状态的概率,算出来哪个最大这个特征值就对应哪个状态.只不过以前是用GMM算的,现在用D ...
- 在虚拟机上的关于FTP FTP访问模式(虚拟用户模式)
首先你要有vsftpd服务 可以先去yum中下载(当然你要有本地yum仓库) 输入命令: yum install vsftpd 下载完成之后打开vsftpd服务 输入命令:systemctl ...
- NOIP模拟 28
果然昨天和别人合照丢的脸今天都加进RP里了 T3是用了dp快速幂(???),T1,T2考试的时候把想法都写注释了. T1: #include<cstdio> using namespace ...
- day2 上午 游戏 对应关系--->判断素数---->多重背包 神题
#include<iostream> using namespace std; int n; ; ]; long long p[maxn]; long long dp[maxn][maxn ...