Python multi-threaded, multi-Process code example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.