1. How to open a process
fromMultiprocessingImportProcessImport TimedefWork (name):#Define a function Print('task <%s> is runing'%name) Time.sleep (1) Print('task <%s> is AAAAA'%name)if __name__=='__main__': P1=process (target=work,args= ('Yxwang',))#produces a process object, followed by the name of the function to be executed. Here, args and a tupleP2=process (target=work,kwargs={'name':'User01'})#You can also use the Kwargs and dictionary method. P1.start ()#execution. is equal to running a child process under the current program. P2.start ()Print('QQQQQQQQQQ')
Execution Result:
2. The second way to open a process
fromMultiprocessingImportProcessImport TimeclassMyprocess (Process):#inherit the Process class def __init__(Self,name): Super ().__init__()#invokes a method in the parent class. Self.name=namedefRun (self):#there must be a way to run. Print('task <%s> is runing'%self.name) Time.sleep (1) Print('task <%s> is AAAAA'%self.name)if __name__=='__main__': P=myprocess ('Egon')#get an objectP.start ()#Run Print('TTTTTT')
Two ways to open a process in Python