In Python, if a method is executed in a multithreaded manner, it is simply a function object that is the first parameter of the synchronization function. However, if the function object is a static method of a class, the function object that uses the class directly will have an error. At this point, you need to construct a proxy method to implement.
For example, the static class method of statistical directory size in the previous blog post, if you want to query the size of multiple directories, and make multi-threaded way. You can use the following methods:
defDir_size_proxy (Cls_instance, i):returncls_instance.get_dir_size (i)classDiskspaceutil (object): @staticmethoddefget_dir_list_size (dir_path_list):ImportMultiprocessing Pool= multiprocessing. Pool (processes=8) Result_list= [] forDir_pathinchdir_path_list:result_list.append (Pool.apply_async (Dir_size_proxy, (Diskspaceutil, Dir_path)) pool . Close () pool.join () size_list= [Result.get () forResultinchResult_list]returnSize_list
Synchronization method The Get_dir_size method in the Diskspaceutil class.
Agent function: Dir_size_proxy
Python static methods in multithreaded execution classes