linux zmq环境配置,zmq安装与使用

Zmq的安装与使用

花了一下午时间来安装使用zmq,终于将程序调通。记录下安装使用过程及遇到的问题

zmq的安装

安装前准备

在安装zeromq之前需要安装libtool, autoconf, automake, uuid-dev, util-linux

具体指令如下

yum install libtool

yum install autoconf

yum install automake

yum install uuid  uuid-devel

安装util-linux时我直接下载的源码包,

地址:http://mirrors.oss.org.cn/linux_kernel/util-linux/v2.21/util-linux-2.21.1.tar.gz

下载后解压安装

tar zxvfutil-linux-2.21.1.tar.gz

cd util-linux-2.21.1

./configure

make

sudo make install

sudo ldconfig

1.2 安装zmq

获得zeromq的源码包

wget http://download.zeromq.org/zeromq-2.0.10.tar.gz

如果想获得最新的源码包http://download.zeromq.org/

安装zeromq

tar zxvf zeromq-2.0.10.tar.gz

cd zeromq-2.0.10

./configure

make

sudo make install

sudo ldconfig

1.3 使用python编程,要安装pyzmq

获得pyzmq的源代码

https://pypi.python.org/packages/source/p/pyzmq/

安装

tar zxvf pyzmq-14.4.0.tar.gz

cd pyzmq-14.4.0

python setup.py build

sudo python setup.py install

注意:pyzmq版本要与zeromq版本一致,不然就算安装了pyzmq运行.py脚本时也会报错,我开始时就是遇到了这个问题,报错如下

Traceback (most recent call last):

File "server.py", line 1, in

import zmq

File "/usr/lib64/python2.6/site-packages/zmq/__init__.py", line 26, in

from zmq.utils import initthreads # initialize threads

ImportError: libzmq.so.0: cannot open shared object file: No such file or directory

从字面意思看,是找不到libzmp.so.0文件,查看官方文档,说是因为

I am guessing you are on on Linux. This looks like the error you will see if you don't use rpath or set LD_LIBRARY_PATH.

If you install libzmq to a location other than the default (/usr/local) on Linux, you may need to do one of the following:

·SetLD_LIBRARY_PATHto point to thelibdirectory of ØMQ.

·Build the extension using the--rpathflag:

$ python setup.py build_ext --rpath=/opt/zeromq-dev/lib

于是按照以下方式设置LD_LIBRARY_PATH

[moqian.ydd@r10g04458.sqa.zmf /home/moqian.ydd/tools]

$cd /

vi ~/.bash_profile

添加:

LD_LIBRARY_PATH=/usr/local/lib

export LD_LIBRARY_PATH

即:

PATH=$PATH:$HOME/bin

LD_LIBRARY_PATH=/usr/local/lib

export PATH

export LD_LIBRARY_PATH

然后运行

$ source ~/.bash_profile

设置后,再运行.py脚本,还是报上面的错误。于是重新安装pyzmq的最新版本,问题得到解决

举例

采用zmq的request/reply模式,使用TCP协议

Client端代码

import zmq

context=zmq.Context()

print "connect to server"

socket=context.socket(zmq.REQ)

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

for request in range(1,10):

print "send message ",request,"...."

socket.send("hello")

message=socket.recv()

print "receive reply ",request,"[",message,"]"

Server端代码

import zmq

import time

context=zmq.Context()

socket=context.socket(zmq.REP)

socket.bind("tcp://*:555")

while True:

message=socket.recv()

print "receive request:",message

time.sleep(1)

print "do some work"

socket.send("i am work!")

开两个客户端,先启动server端脚本,然后启动client端脚本,方式如下

sudo pyton server.py

sudo python client.py

Client端运行结果

connect to server

send message  1 ....

receive reply  1 [ i am work! ]

send message  2 ....

receive reply  2 [ i am work! ]

send message  3 ....

receive reply  3 [ i am work! ]

send message  4 ....

receive reply  4 [ i am work! ]

send message  5 ....

receive reply  5 [ i am work! ]

send message  6 ....

receive reply  6 [ i am work! ]

send message  7 ....

receive reply  7 [ i am work! ]

send message  8 ....

receive reply  8 [ i am work! ]

send message  9 ....

receive reply  9 [ i am work! ]

Server端运行结果如下

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

receive request: hello

do some work

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值