April 24

Today, the teacher talked about a lot of theories. First of all, he said that if a program runs many times, it is multiple processes.

from multiprocessing import Process
import time
import os 
def task():
print('%s: parent process %s'%(os.getpid(),os.getppid()))
time.sleep(3)


if __name__=='__main__':
p=Process (target=task,name='123')
p.start()
print(p.pid)
print(os.getppid())


Imports the multiprocessing module, the processes are physically isolated from each other, and

functions or classes can be used as child process. To open a child process under windows, you should put it below if__name__='__main__'
because the program will be imported when running the child process, and it will not loop infinitely later. The

process is the class
p=Process (target=task, args=('egon',)) to The form of tuple or dictionary) kwargs={'name'='egon'}
p.start() is to send a request to the operating system, the operating system will apply for memory space, and then copy the data of the parent process to the child process as The child process
must have a run method under the class to generate a process. p.start() is calling run
p.join(), which is to keep the parent class waiting, and will also collect the son's body, which will cause blocking
p . .terminate() is to send a request to the operating system to kill the child process. When to kill is the operating system .
p.is_alive is to determine whether the child process is or
os.
os.getPPid is to get the main process pid

















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324807168&siteId=291194637