Centos下安装zmq库及pyzmq

来源:互联网 发布:java多线程视频教程 编辑:程序博客网 时间:2024/05/18 23:26

转载请注明来源: http://blog.csdn.net/lx1848/article/details/50393128

Centos下安装zmq库及pyzmq


1. ZeroMQ简介

ZMQ(ØMQ、ZeroMQ, 0MQ)看起来像是一套嵌入式的网络链接库,但工作起来更像是一个并发式的框架。它提供的套接字可以在多种协议中传输消息,如线程间、进程间、TCP、广播等。你可以使用套接字构建多对多的连接模式,如扇出、发布-订阅、任务分发、请求-应答等。ZMQ的快速足以胜任集群应用产品。它的异步I/O机制让你能够构建多核应用程序,完成异步消息处理任务。ZMQ有着多语言支持,并能在几乎所有的操作系统上运行。ZMQ是iMatix公司的产品,以LGPL开源协议发布。

2. 下载源代码

a. zeromq-3.2.5.tar.gz from http://zeromq.org/intro:get-the-software

b. pyzmq-2.2.0.tar.gz from https://pypi.python.org/pypi/pyzmq/2.2.0


本文实验环境为centos6.3


3. install zeromq-3.2.5

编译源代码需要以下工具,确保linux系统中已经安装:

      uuid-devel、libuuid-devel、gcc、gcc-c++、libtool、make、python、tar

a. 解压

$tar -zxvf zeromq-3.2.5.tar.gz

$cd zeromq-3.2.5

b.  编译

$./autogen.sh 

$./configure # default install to /usr/local/lib

$sudo make -j 4

$sudo make check 

$sudo make install

$sudo ldconfig


编译成功后,默认会在/usr/local/lib产生一个名为libzmq.a的静态文件,在

我们编译自己的代码时,需要以静态库的方式引用该库。例如我们的代码文件

名为wuserver.c,其中用到了zmq,那么编译时用命令:

gcc -o wuserver wuserver.c -lzmq

更多关于zmq的例子可以参考:http://git.oschina.net/junglefire/zguide-cn/blob/master/chapter1.md

3. install pyzmq-2.2.0

a. 解压


$tar -zxvf pyzmq-2.2.0.tar.gz

$cd pyzmq-2.2.0


a. sudo yum install python-devel.i386 (32位包)

# this can sovle problem:Python headers needed to compile C extensions,

# please install development version of Python

# ubuntu下命令为:sudo apt-get install python-dev

b. sudo python setup.py configure --zmq=/usr/local #set the zmq install path

c. sudo python setup.py install


4.附上一个简单例子的代码:

服务器端:zmqserver.py


客户端:zmqclient.py

运行示例:

打开一个shell,执行命令运行服务器端代码: $python zmqserver.py

打开另外一个shell并执行命令运行客户端代码:$python zmqclient.py

输出为:

服务器端输出为:

Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello

客户端输出为:

Connecting to server...
Sending request  1 ...
Received reply  1 [ World from 5556 ]
Sending request  2 ...
Received reply  2 [ World from 5556 ]
Sending request  3 ...
Received reply  3 [ World from 5556 ]
Sending request  4 ...
Received reply  4 [ World from 5556 ]
Sending request  5 ...
Received reply  5 [ World from 5556 ]
Sending request  6 ...
Received reply  6 [ World from 5556 ]
Sending request  7 ...
Received reply  7 [ World from 5556 ]
Sending request  8 ...
Received reply  8 [ World from 5556 ]
Sending request  9 ...
Received reply  9 [ World from 5556 ]


zmq支持多个服务器,所以还可以这样运行以上例子。

shell1: $python zmqserver.py 5546  #sever1

shell2: $python zmqserver.py 5556  #server2

shell3: $python zmqclient.pyt 5546 5556 #send msg to server1 and server2

output;

server1: 

Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello

server2: 

Received request:  Hello
Received request:  Hello
Received request:  Hello
Received request:  Hello

client:

Connecting to server...
Sending request  1 ...
Received reply  1 [ World from 5546 ]
Sending request  2 ...
Received reply  2 [ World from 5556 ]
Sending request  3 ...
Received reply  3 [ World from 5546 ]
Sending request  4 ...
Received reply  4 [ World from 5556 ]
Sending request  5 ...
Received reply  5 [ World from 5546 ]
Sending request  6 ...
Received reply  6 [ World from 5556 ]
Sending request  7 ...
Received reply  7 [ World from 5546 ]
Sending request  8 ...
Received reply  8 [ World from 5556 ]
Sending request  9 ...
Received reply  9 [ World from 5546 ]


0 0
原创粉丝点击