There was a problem that gnaws at me a lot, I can't deal with it for a long time. Describe the situation: There is a class File , which consists of several methods, which in turn perform their inherent operations on the file passed to them (the path to the file). I need to make it so that several instances of the class are created in parallel and then perform some action on them (for simplicity, simply return the class field through this instance). When you run a script, it can once output what is required of it, and if you run this script again without changing anything, it constantly gives an error (which I will describe below). The strings that are passed to the Triplex function are absolutely adequate, without any syntax errors. If you try to pass the Triplex function to a string in the Triplex view (mas [0]), then the code returns what is required of it. Help to understand the error! Thank.
CODE
import magic import os import hashlib import zlib import stat import time from multiprocessing import Pool class File: Hash_md5 = None Hash_sha256 = None Hash_crc32 = None Name = None Path = None Size = None Type_signature = None Type_extension = None Executive = None def __init__(self,path): File.Hash_md5 = self.Hash_md5(path) File.Hash_sha256 = self.Hash_sha256(path) File.Hash_crc32 = self.Hash_crc32(path) File.Name = self.Name(path) File.Path = self.Path(path) File.Size = self.Size(path) File.Type_signature = self.Type_signature(path) File.Type_extension = self.Type_extension(path) File.Executive = self.Executive(path) return None def Hash_md5(self,path): with open(path, 'rb') as file: obj = hashlib.md5() while True: data = file.read(8192) if not data: break obj.update(data) return obj.hexdigest() def Hash_sha256(self,path): with open(path, 'rb') as file: obj = hashlib.sha256() while True: data = file.read(8192) if not data: break obj.update(data) return obj.hexdigest() def Hash_crc32(self,path): prev = 0 for eachLine in open(path,"rb"): prev = zlib.crc32(eachLine, prev) return "%X"%(prev & 0xFFFFFFFF) def Name(self,path): start = path.rfind("/") return path[start+1:] def Path(self,path): return path def Size(self,path): return os.path.getsize(path) #bytes def Type_signature(self,path): return magic.from_file(path) def Type_extension(self,path): filename, file_extension = os.path.splitext(path) return file_extension def Executive(self,path): st = os.stat(path) return bool(st.st_mode & stat.S_IXUSR) def triplex(param): obj0 = File(param) return obj0.Size obj = Pool() mas = ["/root/Downloads/eltekh.pdf","/root/Downloads/control.tar.gz","/root/Downloads/bootstrap-4.1.3-dist.zip","/root/Downloads/templates","/root/Downloads/mysql-connector-python-8.0.12.zip"] result = obj.map(triplex, mas) print(result) MISTAKE
multiprocessing.pool.RemoteTraceback: """ Traceback (most recent call last): File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/usr/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar return list(map(*args)) File "main_file.py", line 92, in triplex obj0 = File(param) File "main_file.py", line 20, in __init__ File.Hash_md5 = self.Hash_md5(path) TypeError: 'str' object is not callable """ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "main_file.py", line 100, in <module> result = obj.map(triplex, mas) File "/usr/lib/python3.6/multiprocessing/pool.py", line 288, in map return self._map_async(func, iterable, mapstar, chunksize).get() File "/usr/lib/python3.6/multiprocessing/pool.py", line 670, in get raise self._value TypeError: 'str' object is not callable
maslist, for example, the first 3, then the program runs as always. If you pass 4 arguments, the program starts running once (that is, when it is first started, all buzzes, with the second an error flies, with the third again an error, with the fourth, all buzz, etc.) - Martin OrlovFile.Hash_md5 = self.Hash_md5(path); what do you think should do? - Fat-Zer 6:26 pmFile.Hash_md5is also an object method and this assignment replaces the method with a string, and does it for all instances class. - Fat-Zer