The premise is that sometimes a process is used to handle each element of a list (each element is passed to a function for processing), and this time it is processed with a multi-process
1 Field Case:
I have a [IP1,IP2,IP3,.......] Such a list, I want each element IP passed to a get_ping_info (addr) function to get the return delay information, and then the result to a saved result list, if it takes a single process to execute it may take a few minutes, but if multi-process processing can be reduced several times the speed of
Usage: (Program code only part, not run)
Pool.apply_async (function name, (parameter 1 of functions, parameter 2 of function, parameter of function 3))
Import multiprocessing
def get_ping_info (create_time,prov,city,net_type,addr): result = ping.quiet_ping (addr, timeout=2, count=5, psize=64) Loss_rate = result[0] Max_time = float ('%.3f '% result[1]) if Isinstance (result[1], float) ELSE 0 #if Max_time and a Verage_time is None use 0 average_time = float ('%.3f '% result[1]) if Isinstance (result[2], float) ELSE 0 print creat E_time,prov,city,net_type,loss_rate, Average_time return (create_time,prov,city,net_type,loss_rate, average_time)
if __name__ = = "__main__": all_city_result_tmp = [] All_city_result = [] Create_time = DateTime.Now () city_lis t = get_city_list () pool = multiprocessing. Pool (processes=4) for prov,city,addr in City_list:all_city_result_tmp.append (Pool.apply_async (Get_ping_info, (c Reate_time,prov,city, ' CTC ', addr)) Pool.close () Pool.join () for City_info in all_city_result_tmp: #print City_info.get () All_city_result.append (City_info.get ())
Attention:
1 all_city_result_tmp is just a multithreaded multiline list of elements in the list. The Get () function does not execute the element object, and the object has information and return values that execute the Get_ping_info function.
This article is from the "Tridewah operation and maintenance work Road" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1764707
Python multi-process processing multiprocessing module