There are two ways of multithreading: Thread and threading
The scenario used here is a sample of how the map data is split into multiple threads and the progress is written Codis
This is an example of thread : the main process of thread does not wait for threads
ImportThread,math,threading,multiprocessing,os,timedefWritetocodis (prefix,key_list,result_map):#client = Bfdcodis ("xxx",)Begin =Int (time.time ()) forKeyinchkey_list:PrintKey#client.set (prefix + key, Result_map[key]) #client.expire (prefix + key, 1 * 3600)End =Int (time.time ())#Assign keyGidtosid={1:2,3:4}total_key=Len (gidtosid) Gap= Int (Math.ceil (float (total_key)/process_num)) Keys=Gidtosid.keys () forIinchRange (0,process_num): Thread.start_new_thread (Writetocodis, (G2s_prefix,keys[i*gap: (i+1) *gap],gidtosid))#T.start ()Time.sleep (10)
Threading will wait for the thread to execute
for inch Range (0,process_num): t=threading. Thread (target=writetocodis,args= (g2s_prefix,keys[i*gap: (i+1) *gap],gidtosid)) T.start ()
This is an example of a multi-process
for inch Range (0,process_num): = multiprocessing. Process (target=writetocodis,args= (g2s_prefix,keys[i*gap: (i+1) *gap],gidtosid)) P.start ()
Python multi-threaded, multi-Process code example