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

时间:2018-05-04 09:47:50

标签: python google-drive-api pydrive

对我的英文抱歉。我使用pyDryve 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)

1 个答案:

答案 0 :(得分:2)

我最近遇到了相同的错误,事实证明这是httplib2库的问题,如Google在本thread safety指南中所述。

  

google-api-python-client库建立在httplib2的顶部   库,它不是线程安全的。因此,如果您以   多线程应用程序,您正在请求的每个线程   from必须具有自己的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)