以下代码适用于txZMQ 0.7.0,但在txZMQ 0.7.3上如下所示。有什么问题?这是txZMQ中的错误。
这是测试代码:
#!/usr/bin/env python
from twisted.internet import reactor
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPubConnection
import msgpack
zf = ZmqFactory()
e = ZmqEndpoint('bind', 'tcp://*:5557')
s = ZmqPubConnection(zf, e)
def publish():
data = [35, 11, 20, 4, 49, 1, 1, 49]
msgpack_data = msgpack.packb(data)
print "publishing %r" % data
s.publish(msgpack_data)
reactor.callLater(1, publish)
reactor.run()
使用txZMQ 0.7.0:
pivert@pivert-desktop:~/tmp/twisted$ ./example.py
publishing [35, 11, 20, 4, 49, 1, 1, 49]
Unhandled Error
Traceback (most recent call last):
File "./example.py", line 28, in <module>
reactor.run()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1192, in run
self.mainLoop()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1201, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 824, in runUntilCurrent
call.func(*call.args, **call.kw)
File "./example.py", line 24, in publish
s.publish(msgpack_data)
File "/usr/local/lib/python2.7/dist-packages/txzmq/pubsub.py", line 26, in publish
self.send(tag + b'\0' + message)
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0x98 in position 0: ordinal not in range(128)
然后,我将txZMQ降级到0.7.0:
root@pivert-desktop:~# pip install txZMQ==0.7.0
并重新执行:
pivert@pivert-desktop:~/tmp/twisted$ ./example.py
publishing [35, 11, 20, 4, 49, 1, 1, 49]
答案 0 :(得分:0)
这可能是个问题。
https://github.com/smira/txZMQ/issues/56
我通过在我对publish()
的调用中添加tag = b'来解决这个问题e.g:
s.publish(msgpack_data, tag=b'')