2

对不起我的英语。我将 pyDryve 用于谷歌驱动器 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)
4

1 回答 1

3

我最近遇到了同样的错误,结果证明是 httplib2 库的问题,正如google在这个线程安全指南中所解释的那样。

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)
于 2019-03-21T14:23:15.270 回答