The process of explanation

Source: Internet
Author: User

1. Object-oriented __setitrm__ getitem__

1 classFoo (object):2     def __init__(self):3self.info={}4     def __setitem__(self, Key, value):5self.info[key]=value6     def __getitem__(Self, item):#Item is key7         Print(item)8         returnSelf.info[item]9obj=Foo ()Tenobj["C"]=233#It's just a simple dictionary. One Print(obj["C"])

2. Object-oriented __setattr__,__getattr__

1 classFoo (object):2     def __init__(self):3Object.__setattr__(Self,"Info",{})#the method of inner nature of module4     def __setattr__(self, Key, value):5self.info[key]=value6     def __getattr__(Self, item):#the item here is almost key.7         Print(item)8         returnSelf.info[item]9obj=Foo ()TenObj.name="Alex"#Here's the quote . One Print(Obj.name)

Two processes

1. The process can not pass data, but also has a master process, the default Deamon=flase master process can be completed waiting for the child process

1 ImportMultiprocessing2 ImportThreading3lst=[]4 deffunc (i):5     #print ("Verify can be data between")6 lst.append (i)7     Print(LST)8 defrun ():9      forIinchRange (10):TenT1=multiprocessing. Process (target=func,args=(i,)) One T1.start () A if __name__=='__main__': -Run ()

2. The process can also be divided into two ways, one is to introduce the module multiprocseeing, and the other is to inherit the parent class

class Foo (multiprocessing. Process):    def  Run (self):        a=multiprocessing.current_process ()          Print(" process%s" % a)def  Run ():    obj=  Foo ()    Obj.start ()if__name__'__main__':    Run ()

3. Processes can share information through queue and manager

The queue may not run on Windows

1 ImportMultiprocessing2 ImportThreading3 ImportQueue4 Import Time5q=multiprocessing. Queue ()6 defTesk (i,q):7     8q.put= (i)#queues are used to restrict9 defRun:Ten      forIinchRange (10): OneT1=multiprocessing. Process (target=tesk,args=(I,q,)) A T1.start () -      while1: -V1=q.get ()#Here's a loop . the         Print(v1) - if __name__=='__main__': -Run ()

Manager

Block in the form of a dictionary

1 defTesk (arg,dic):2Time.sleep (5)3dic[arg]=1004 if __name__=='__main__':5m =multiprocessing. Manager ()6Dic=m.dict ()7lst=[]8      forIinchRange (10):9T1=multiprocessing. Process (target=tesk,args=(i,dic))Ten T1.start () One lst.append (t1) A      while1:#used to make a judgment. Otherwise, even if the program runs, it doesn't end. -con=0 -          forIinchLst:#Here I is the process of each cycle the             if  notI.is_alive ():#If the process is not present, the instructions have been run, because the child process is not finished, -                 #If it ends early, it will be an error -Con+=1 -         ifCon==len (LST):#know that equality is not stopped +              Break -     Print(DIC)

4. Process Lock

1lock=multiprocessing. Rlock ()2 defTesk (i):3     Print("the Japs are here.")4 Lock.acquire ()5Time.sleep (2)6     Print(i)7 lock.release ()8 if __name__=='__main__':9T1=multiprocessing. Process (target=tesk,args= (1,))Ten T1.start () OneT2=multiprocessing. Process (target=tesk,args= (1,)) AT2.start ()

5. Process Pool

1  fromConcurrent.futuresImportThreadpoolexecutor,processpoolexecutor2 defTesk (i):3Time.sleep (5)4     Print("really want%s"%i)5 if __name__=='__main__':6M=processpoolexecutor (5)7      forIinchRange (5):8M.submit (Tesk,i)

The process of explanation

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.