json.loads和pickle.loads不能与async关键字一起使用吗?

时间:2018-07-31 22:18:44

标签: json python-3.x python-asyncio pyzmq

我有一台zmq服务器,它可以订阅多个发布者(但在此示例中仅订阅一个发布者)

在通过创建协程使服务器异步并收到带有pickle.loadsjson.loads的序列化多部分消息反序列化之后,两者似乎均不起作用。 (我什至没有收到错误消息)

当我在没有异步协程序列化的情况下创建我的应用程序时,它就像一个魅力。如果我省略反序列化,则服务器也可以正常运行。

所以我想知道是否出于某种原因需要将json和pickle api用于asyncio,如果是这样:通常如何完成?

async def receiver():
    print("1")
    socket =  ctx.socket(zmq.SUB)
    with ctx.socket(zmq.SUB) as socket:
        print("2")
        socket.setsockopt(zmq.SUBSCRIBE, b"")
        print("3")
        for url in urls:
            socket.connect(url)
        print("4")
        poller = Poller()
        poller.register(socket, zmq.POLLIN)
        while True:
            events = await poller.poll()
            if socket in dict(events):
                print("5")
                items = await socket.recv_multipart()
                res = b"".join(items)
                s = res.decode(encoding="utf-8")
                print(s)
                t = json.loads(s)
                print(t)
                print("6")

如果我省略行t = json.loads(s)print(t),我将得到输出6,当收到更多消息时,我将得到输出56对于每个收到的消息。如果我保留这些行,则不会得到输出6,也不会再收到任何消息

发布者代码基本上看起来像这样:

ctx = zmq.Context()
socket = ctx.socket(zmq.PUB)
socket.bind(addr)

f = open("foo.json", "r")
for line in f:
    jsn = json.loads(line)
    for el in jsn["somekey"]["someOtherKey"]:
        data = {"data":el,"some more metadata":"here is the meta data"}
        b = json.dumps(data).encode()
        #b = pickle.dumps(data,protocol=pickle.HIGHEST_PROTOCOL)
        socket.send_multipart(b)

我应该声明发布者在python2上运行,接收者在python3上运行。发布者代码将无法在python3上运行,因为我收到了python3的以下堆栈跟踪信息(这对我也没有意义)

 Traceback (most recent call last):
 File ".../python3.6/site-packages/zmq/sugar/socket.py", line 356, in send_multipart
    _buffer_type(msg)
 TypeError: memoryview: a bytes-like object is required, not 'int'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
 File "Publisher2.py", line 39, in <module>
    main("tcp://127.0.0.1:5556", "mockPublisher")
 File "Publisher2.py", line 28, in main
    socket.send_multipart(b)
 File ".../python3.6/site-packages/zmq/sugar/socket.py", line 363, in send_multipart
    i, rmsg,
 TypeError: Frame 0 (123) does not support the buffer interface.

0 个答案:

没有答案