SoFunction
Updated on 2024-12-10

Python's thread pool map method passes multi-parameter list

The thread pool map() method passes a multi-parameter list

Before the booster interface through () for multi-threaded concurrency, but this for the number of concurrency is relatively small when it is better to use, if the number of concurrency, in addition to the thread package concatenation of this treatment in the case, we can also use thread pooling methods.

Thread pooling implementation in layman's terms is to put all the tasks in the message queue, open multiple threads after the execution of the thread, but the end of the thread execution will not interrupt the thread task, will continue to get threads from the message queue task for thread execution, so that thread pooling than the multi-threaded operation saves a lot of steps to create a thread and close the thread to save most of the resources and time.

Thread pool concurrency requires the introduction of the module

import 

ThreadPoolExecutor within the two thread pooling methods map () and submit () today first say map () method

His syntax is

 with () as pool:
      res = (craw, uid_list)
      print(res)
  • map()The inner craw is the method name, here the method is not named with ()
  • uid_listAs a method parameter, the list datatype needs to be passed inside the map() method.

Let's look at the overall code first

5000 users concurrently powering

    def test_case_09(self):
        """5000 users concurrently boosted"""
        # Get uid_list via yaml configuration file wrapper method
        uid_list = YamlHandler(YamlThePath().number_new).get_uid_list()
        # add_ticket Get 5000 account login status
        with () as pool:
            (AccountAccess().add_ticket, uid_list)
        # 5000 Account Thread Pool Methods to Help Users
        with () as pool:
            (PreheatMethod().help, [(uid, , 1) for uid in uid_list])
        # of times a user has been boosted
        response = PreheatMethod().init()
        print(f"Number of times the current user has been boosted :{response['data']['userInfo']['helpedCount']}substandard")

Take a look at the methods of the two interfaces again to get a better sense of them

The first step is to get the login status add_ticket

    def add_ticket(self, uid):
        """
        Get individual user t-ticket
        :param uid: individual user uid
        :return.
        """
        ['url'] = ApiAddress().get_ticket
        ['host'] = ApiAddress().host
        ['uid'] = str(uid)
        ['type'] = 0
        ['params'] = ()
        res = r().post(url=ApiAddress().ticket, data=)
        print(f'gaintvoting result:{uid}{res}')
        return uid

It's a simple interface request. The input is a uid, but note that the uid is not a list, it's just a parameter.

Then some students have questions about the method parameter passed within map() is a list of uid contents.

The map() method just takes the parameters you need in a list and iterates through them to request the interface you specify.

At this point some of you may ask again, because I was asking myself the same thing, what if there are multiple parameters within a method, where some of these parameters are not even fixed content.

Let's take a look at another method that requests a booster interface

    def help(self, agrs):
        """
        Boosted Users
        :param agrs: uid:current user uid to_uid:booster uid count:number of boosts
        :return.
        """
        uid, to_uid, count = agrs
        ['toUid'] = str(to_uid)
        ['count'] = count
        response = r().response(uid, , "help", **)
        (f'help response uid:{uid} to_uid:{to_uid}\n{response}')
        return response

That's right, we pass it inside the booster interface by means of a tuple, assigning values to each of the specified elements by means of keyword positions within the tuple.

Then within the thread pool code, we facilitate the parameters within the uid_list to your specified tuple through the list derivation, of course, here if it is more than one parameter, you can also use the dictionary, the dictionary facilitates the key and value as a change in the parameters, because the list derivation gives you the return of the list, so we need the parameters within the tuple, the tuple is placed within the list, so that the You can use the map() thread pool for concurrent methods with multiple parameters.

with () as pool:
            (PreheatMethod().help, [(uid, , 1) for uid in uid_list])
[(uid, , 1) for uid in uid_list]

The list deduced from the list is roughly in the form of the list data content below

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.