美文网首页
用Python操作nanomsg(一)——准备

用Python操作nanomsg(一)——准备

作者: 钢琴师2005 | 来源:发表于2020-02-07 22:39 被阅读0次

日前因工作需要,整在一点一点熟悉开源跨平台消息中间件:nanomsg,恰逢最近安装了Typora用于练习Markdown语法,那就一并把学习总结整理记录下来并同步更新到简书方便他人和自己日后回看。

关于nanomsg

nanomsg.org
nanomsg是一个消息通信组件,由zeroMQ的作者之一用C语言重写的通信框架,可以简单理解为跟Socket一样的东西,不过相比Socket它有很多优秀的特性,目前在各开发语言中都有相应的wrapper,其C、C++和Python的wrapper如下:
语言 库地址 说明
C nanomsg Reference implementation, sustaining mode
NNG Modern re-implementation.
C++ nanomsgxx
cppnanomsg
nngpp (NNG binding)
Python nanomsg-python
nnpy
pynng NNG binding. Docs here.

nnpy是其中一个对nanomsg的python wrapper,相比于nanomsg-python日渐缺少维护,更推荐使用nnpy。另外,现在也有了nng(nanomsg next negeration),当nanomsg使用熟练后可考虑转nng。

安装

本文基于Pyhton3.7,当前nnpy的最新版本为1.4.2,依次安装cmake、nanomsg、cffi和nnpy:

#安装cmake
sudo apt-get install cmake
#安装nanomsg
git clone https://github.com/nanomsg/nanomsg.git
cd nanomsg
mkdir build
cd build
cmake ..
cmake --build .
ctest -C Debug .
sudo cmake --build --target install
sudo ldconfig
#先安装cffi,版本要求为>=1.0
pip3 install cffi
#安装nnpy
pip3 install nnpy

配置开发环境

这里使用的开发环境为Jetbrains Pycharm 2019 + WSL,WSL使用的是Kali-Linux,其他版本如Ubuntu、Debian等也都可以。

设置WSL为Python Interpreter

我本机装的是Python 3.6,点击右下角当前正在使用的本地解析器名称Python 3.6,选择Add Interpreter

选择Add Interpreter

从左侧选择WSL后,右侧面板自动出来当前的WSL发行版本,注意的是这里默认的解析器路径为/usr/bin/python,Kali-Linux默认安装的时候只有python3没有python,需要手动改为/usr/bin/python3

修改为可用的python路径(这里为/usr/bin/python3)

而后点击OK完成WSL Interpreter的添加,在右下角选择3.7@Kali Linux即可启用WSL作为远程开发环境——不需要SSH、虚拟机或VPS就能在Windows下进行Linux开发,简直不要太舒服!!

选择刚才创建的WSL环境进行开发

nanomsg通信模式

nanomsg提供了如下几种通信模式,太具体的不介绍,说完会用就明白是怎么回事儿了:

PipeLine

  • aggregates messages from multiple sources and load balances them among many destinations

  • 单向管道模式,只能从一个node发往另一个node,不能临时对调角色

PushPub

  • distributes messages to large sets of interested subscribers

  • 发布/订阅模式,发布者可以广播消息,订阅者可以接收消息并且可选订阅消息的前缀,发布者发布信息与订阅者状态无关,订阅者如果不在线则会错过发布者在其不在线时刻发布的消息,上线时则不会错过任何消息

Pair

  • simple one-to-one communication

  • 一对一模式,一个node只能连接一个node,双向通信(send阻塞、recv不阻塞)

ReqRep

  • allows to build clusters of stateless services to process user requests

  • 请求/回复模式,每个request请求都要有与之对应的report相应

Survey

  • allows to query state of multiple applications in a single go

  • 调查模式,看官网介绍类似于PushPub的双向通信版本,允许一次查询多个应用的状态(暂时还没理解)

Bus:

  • simple many-to-many communication

  • 总线模式,一个总线上可以有多个套接字(即是server也是client),在总线上只要有消息产生都能收到,自己产生的消息除外(可理解为多对多通信)。

关于各通信模式的验证请前往本系列后续文章:

主题 文章地址 说明
PipeLine 用Python操作nanomsg(二)——PipeLine 2020.2.7更新
PushPub 用Python操作nanomsg(三)——PubSub 2020.2.8更新
Pair 用Python操作nanomsg(四)——Pair 未开始
ReqRep 用Python操作nanomsg(五)——ReqRep 未开始
Survey 用Python操作nanomsg(六)——Survey 未开始
Bus 用Python操作nanomsg(七)——Bus 未开始

相关文章

网友评论

      本文标题:用Python操作nanomsg(一)——准备

      本文链接:https://www.haomeiwen.com/subject/jkxbxhtx.html