Python static methods in multithreaded execution classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.