celery传参时的对象转换

发布时间 2023-04-24 14:52:09作者: 腐汝

遇到一个有趣的问题,celery delay传入SSH的对象时,报错Object of type SSH is not JSON serializable,分析一下就是只能传json的数据。把所有传入的数据都转成json。

1、因为我传递的是对象,所以要把对象转成json,所以我就自定义了一个JSONEncoder

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, SSH):
            obj.__dict__['arguments']['pkey'] = None
            return obj.__dict__

2、在异步任务里面又将json转成对象

@app.task(name='receive_execute_command', base=CustomTask)
def execute_remote_command(client: json, scripts: str):
    """
    异步执行远程脚本
    :param client:
    :param scripts:
    :return:
    """
    client_json_to_dict = json.loads(client)
    client_json_to_dict['arguments']['pkey'] = SSH.get_private_key()

    new_client_obj = SSH(hostname='none', password='none')
    new_client_obj.__dict__ = client_json_to_dict

    res = new_client_obj.exec_command(scripts, timeout=5)
    return res[1].decode('utf-8')

3、这是调用异步的片段

        elif is_async:
            client_to_json = json.dumps(client, cls=MyEncoder, indent=4)
            execute_remote_command.delay(client_to_json, scripts)
            return ['0', '异步执行中。'.encode('utf-8')]

4、总结下来难点就是对象与json之间的互转。希望能帮助到各位大侠。