多处理-ChicklingError:can can can pickle< class' botocore.client.s3'>>>:属性查找botocore.client on botocore.client失败

发布于 2025-01-30 15:06:30 字数 1017 浏览 5 评论 0原文

以下代码获取错误

_pickle.picklingerror:不能泡菜< class'botocore.client.s3'>:botocore on botocore.client.client失败

import boto3
import concurrent.futures as cf

class Boto3:
    def __init__(self) -> None:
        self.s3client = boto3.client("s3")

class C:
    def __init__(self, s3) -> None:
        self.s3 = s3  # this instance variable may have caused the problem
        
    def f(self, a):
        x = self.s3
        return a*a

c = C(Boto3())

p = [1,2,3]

def run(f, p):
    executor = cf.ProcessPoolExecutor(max_workers=3)
    result = executor.map(f, p)
    print([x for x in result])

if __name__ == "__main__":
    run(c.f, p)

是解决问题的方法吗?


但是,使用全局变量和函数有效吗?

s3 = boto3.client("s3")

def g(a):
    x = s3

....
    result = executor.map(g, p)

另外,如果s3client是类变量,则可以工作。

class Boto3:
    s3client = boto3.client("s3")

The following code gets the error of

_pickle.PicklingError: Can't pickle <class 'botocore.client.S3'>: attribute lookup S3 on botocore.client failed

import boto3
import concurrent.futures as cf

class Boto3:
    def __init__(self) -> None:
        self.s3client = boto3.client("s3")

class C:
    def __init__(self, s3) -> None:
        self.s3 = s3  # this instance variable may have caused the problem
        
    def f(self, a):
        x = self.s3
        return a*a

c = C(Boto3())

p = [1,2,3]

def run(f, p):
    executor = cf.ProcessPoolExecutor(max_workers=3)
    result = executor.map(f, p)
    print([x for x in result])

if __name__ == "__main__":
    run(c.f, p)

Is it a way to resolve the issue?


However, using global variable and functions works?

s3 = boto3.client("s3")

def g(a):
    x = s3

....
    result = executor.map(g, p)

Also, it works if s3client is a class variable.

class Boto3:
    s3client = boto3.client("s3")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文