Quantify programming - multi-threaded and multi-process

# - * - Coding: UTF-8 - * - 
# @date: 2017-08-26 
# @Original: 

# multiprocess 
Import itertools
 from concurrent.futures Import ProcessPoolExecutor 
the Result = [] 

# callback function call to complete the task by add_done_callback 
DEF when_done (r):
     # when_done running in the primary process 
    result.append (r.result ()) 

"" " 
    with class_a () AS A: context Manager 
" "" 
with ProcessPoolExecutor () AS the pool: 
    for keep_stock_threshold, buy_change_threshold in  \
            The itertools.product (keep_stock_list, buy_change_list):

        "" " 
            Submit to submit the task: use calc functions and parameters submitted through submit separate process 
            jobs submitted must be simple function, parallel to the process does not support class methods, closures and other 
            function arguments and return value must be compatible pickle serialization, interprocess communication needs 
        "" " 
        future_result = pool.submit (calc, keep_stock_threshold, 
                                    buy_change_threshold) 
        # when the process is finished after the end of the callback function that is run calc 
        future_result.add_done_callback (when_done)
 Print (sorted (the Result) [:: - 1] [ : 10 ]) 



# multithreaded 
from concurrent.futures Import the ThreadPoolExecutor 

Result = []
 DEF when_done (R & lt): 
    result.append (r.result ())

with ThreadPoolExecutor(max_workers=8) as pool:
    for keep_stock_threshold, buy_change_threshold in \
            itertools.product(keep_stock_list, buy_change_list):
        future_result = pool.submit(calc, keep_stock_threshold,
                                    buy_change_threshold)
        future_result.add_done_callback(when_done)

 

Guess you like

Origin www.cnblogs.com/fangbei/p/11521512.html