miketeo/pysmb

ThreadPoolExecutor storeFile

Zoktog opened this issue · 1 comments

Hello, I'm trying to write many files multi-threaded to a directory, but as soon as there are more than 1 threads, copying does not occur. Can this be done somehow?

        with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
            results = executor.map(lambda file_path: function(smb_client,
                                                             share_name,
                                                             file_path_local=file_path,
                                                             file_path_remote=file_path_remote), items, timeout=120)

def write_file(self, share_name: str, file_path_local: str, file_path_remote: str, ) -> None:
    with open(file_path_local, 'rb') as file:
                file_name = pathlib.Path(file_path_local).name
                self.conn.storeFile(share_name,  str(pathlib.PurePosixPath(file_path_remote, file_name)), file)
    print(f'file {file_name} was copied')

My suggestion is to create one pysmb instance for each thread, so each thread will use its own pysmb instance (and underlying SMB connection) to perform your file transfer operation.