in Education by
I have not seen clear examples with use-cases for Pool.apply, Pool.apply_async and Pool.map. I am mainly using Pool.map; what are the advantages of others? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Back in the old days of Python, to call a function with arbitrary arguments, you would use apply: apply(f,args,kwargs) apply still exists in Python2.7 though not in Python3, and is generally not used anymore. Nowadays, f(*args,**kwargs) is preferred. The multiprocessing.Pool modules tries to provide a similar interface. Pool.apply is like Python apply, except that the function call is performed in a separate process. Pool.apply blocks until the function is completed. Pool.apply_async is also like Python's built-in apply, except that the call returns immediately instead of waiting for the result. An AsyncResult object is returned. You call its get() method to retrieve the result of the function call. The get() method blocks until the function is completed. Thus, pool.apply(func, args, kwargs) is equivalent to pool.apply_async(func, args, kwargs).get(). In contrast to Pool.apply, the Pool.apply_async method also has a callback which, if supplied, is called when the function is complete. This can be used instead of calling get(). For example: import multiprocessing as mp import time def foo_pool(x): time.sleep(2) return x*x result_list = [] def log_result(result): # This is called whenever foo_pool(i) returns a result. # result_list is modified only by the main process, not the pool workers. result_list.append(result) def apply_async_with_callback(): pool = mp.Pool() for i in range(10): pool.apply_async(foo_pool, args = (i, ), callback = log_result) pool.close() pool.join() print(result_list) if __name__ == '__main__': apply_async_with_callback() may yield a result such as [1, 0, 4, 9, 25, 16, 49, 36, 81, 64] Notice, unlike pool.map, the order of the results may not correspond to the order in which the pool.apply_async calls were made. So, if you need to run a function in a separate process, but want the current process to block until that function returns, use Pool.apply. Like Pool.apply, Pool.map blocks until the complete result is returned. If you want the Pool of worker processes to perform many function calls asynchronously, use Pool.apply_async. The order of the results is not guaranteed to be the same as the order of the calls to Pool.apply_async. Notice also that you could call a number of different functions with Pool.apply_async (not all calls need to use the same function). In contrast, Pool.map applies the same function to many arguments. However, unlike Pool.apply_async, the results are returned in an order corresponding to the order of the arguments.

Related questions

0 votes
    When should you apply “next” statement in R? When is it appropriate to use the “next” statement in R?...
asked Oct 16, 2020 in Technology by JackTerrance
0 votes
    My scenario is this: I've got worker which enqueues tasks into a multiprocessing.Queue() if said is empty. This is to ... which pop from the mp.Queue and do some stuff. Sometimes (...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    (I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    (I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 7, 2022 in Education by JackTerrance
0 votes
    (I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 6, 2022 in Education by JackTerrance
0 votes
    I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the ... same thing? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I'm using TensorFlow Alpha 2.0. I have TFRecords files I'm reading from, each one holding a short ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    i am working on a MVC project in java. For my operation I use database many times. many times my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    As I understand it, anything created with an alloc, new, or copy needs to be manually released. For ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Which feature of java 8 enables us to create a work stealing thread pool using all available processors at ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is advantage of using JDBC connection pool? (a) Slow performance (b) Using more memory (c) ... & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Short version: How would i go about creating an object pool that can store classes of different types all ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    We have a few databases in Pricing Tier: Basic, S0... like below picture: These databases were created before a ... the Azure portal. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Where is String Pool stored? (a) Java Stack (b) Java Heap (c) Permanent Generation (d) Metaspace ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which feature of java 8 enables us to create a work stealing thread pool using all available processors at ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...