gpt4 book ai didi

python - MessagePack 和日期时间

转载 作者:太空狗 更新时间:2023-10-30 02:19:23 24 4
gpt4 key购买 nike

我需要一种在 python 多处理进程之间通过 zeromq 每秒发送 300 条短消息的快速方法。每条消息都需要包含一个 IDtime.time()

msgpack 似乎是在通过 zeromq 发送之前序列化 dict 的最佳方式,而且方便的是,msgpack 有一个正是我需要的例子,除了它有一个 datetime.datetime.now()

import datetime

import msgpack

useful_dict = {
"id": 1,
"created": datetime.datetime.now(),
}

def decode_datetime(obj):
if b'__datetime__' in obj:
obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
return obj

def encode_datetime(obj):
if isinstance(obj, datetime.datetime):
return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
return obj


packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)

问题是他们的例子不工作,我得到这个错误:

    obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
KeyError: 'as_str'

可能是因为我在 python 3.4 上,但我不知道 strptime 有什么问题。非常感谢您的帮助。

最佳答案

鉴于 messagepack (import msgpack) 擅长序列化整数,我创建了一个仅使用整数的解决方案:

_datetime_ExtType = 42

def _unpacker_hook(code, data):
if code == _datetime_ExtType:
values = unpack(data)

if len(values) == 8: # we have timezone
return datetime.datetime(*values[:-1], dateutil.tz.tzoffset(None, values[-1]))
else:
return datetime.datetime(*values)

return msgpack.ExtType(code, data)


# This will only get called for unknown types
def _packer_unknown_handler(obj):
if isinstance(obj, datetime.datetime):
if obj.tzinfo:
components = (obj.year, obj.month, obj.day, obj.hour, obj.minute, obj.second, obj.microsecond, int(obj.utcoffset().total_seconds()))
else:
components = (obj.year, obj.month, obj.day, obj.hour, obj.minute, obj.second, obj.microsecond)

# we effectively double pack the values to "compress" them
data = msgpack.ExtType(_datetime_ExtType, pack(components))
return data

raise TypeError("Unknown type: {}".format(obj))

def pack(obj, **kwargs):
# we don't use a global packer because it wouldn't be re-entrant safe
return msgpack.packb(obj, use_bin_type=True, default=_packer_unknown_handler, **kwargs)


def unpack(payload):
try:
# we temporarily disable gc during unpack to bump up perf: https://pypi.python.org/pypi/msgpack-python
gc.disable()
# This must match the above _packer parameters above. NOTE: use_list is faster
return msgpack.unpackb(payload, use_list=False, encoding='utf-8', ext_hook=_unpacker_hook)
finally:
gc.enable()

关于python - MessagePack 和日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30313243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com