谷歌驱动多线程移动文件PYTHON

问题描述 投票:1回答:1

对不起我的英文。我使用gDrive for google drive api。我想将文件从一个文件夹移动到另一个文件夹,用于此多线程。

        pool = ThreadPoolExecutor(max_workers=2)
# i have list of file
 for file in date_val:
                    pool.submit(self.start_rename_move_process, file)


def start_rename_move_process(self, file):
    try:
        print(file['title'])

        # Retrieve the existing parents to remove
        move_file = thread_drive.g_drive.auth.service.files().get(fileId=file['id'],
                                                          fields='parents').execute()

        previous_parents = ",".join([parent["id"] for parent in move_file.get('parents')])

        # Move the file to the new folder
        thread_drive.g_drive.auth.service.files().update(fileId=file['id'],
                                                       addParents=MOVE_FOLDER_ID,
                                                       removeParents=previous_parents,
                                                       fields='id, parents').execute()

    except BaseException as e:
        print(e)

我有错误:

[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2217)

我的问题:为什么在一个线程中一切正常,但如果我做2线程我有错误

[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2217)
python google-drive-sdk pydrive
1个回答
2
投票

我最近遇到了同样的错误,结果证明这是httplib2库的一个问题,正如谷歌在这个thread safety指南中所解释的那样。

google-api-python-client库构建在httplib2库之上,该库不是线程安全的。因此,如果您作为多线程应用程序运行,则您发出请求的每个线程都必须具有自己的httplib2.Http()实例。

该指南为此问题提供了两种解决方案:

为线程提供自己的httplib2.Http()实例的最简单方法是在服务对象中覆盖它的构造,或者通过http参数将实例传递给方法调用。

# Create a new Http() object for every request
def build_request(http, *args, **kwargs):
    new_http = httplib2.Http()
    return apiclient.http.HttpRequest(new_http, *args, **kwargs)
service = build('api_name', 'api_version', requestBuilder=build_request)

# Pass in a new Http() manually for every request
service = build('api_name', 'api_version')
http = httplib2.Http()
service.stamps().list().execute(http=http)
© www.soinside.com 2019 - 2024. All rights reserved.