2

Sorry fro my englihs. I use pyDryve for google drive api. And i want move files from one folder to another, use for this multythreading.

        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)

i have error:

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

My question: Why in one thread all works fine, but if i do 2 thread i have error

[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2217)
nesalexy
  • 848
  • 2
  • 9
  • 30
  • Possible duplicate of [SSL: WRONG\_VERSION\_NUMBER when setting up public Juypter server](https://stackoverflow.com/questions/34345605/ssl-wrong-version-number-when-setting-up-public-juypter-server) – Linda Lawton - DaImTo May 04 '18 at 11:47
  • @DaImTo man, it looks like, but not that. I work with google drive sdk and cant understand why in one thread all works fine, but if i do 2 thread i have error – nesalexy May 04 '18 at 11:58

1 Answers1

3

I've run into the same error recently, it turned out to be a problem with the httplib2 library as explained in this thread safety guide by google.

The google-api-python-client library is built on top of the httplib2 library, which is not thread-safe. Therefore, if you are running as a multi-threaded application, each thread that you are making requests from must have its own instance of httplib2.Http()

The guide provided two solutions for this problem:

The easiest way to provide threads with their own httplib2.Http() instances is to either override the construction of it within the service object or to pass an instance via the http argument to method calls.

# 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)
AMasrar
  • 51
  • 6
  • 1
    Note that the thread safety guide has moved to: https://github.com/googleapis/google-api-python-client/blob/master/docs/thread_safety.md – Michael Sep 30 '20 at 16:22