- Because the fork creation process can no longer be used on Windows systems, multiprocessing is generated. Process
- Process can be instantiated directly and then called with start, you need to specify the function that the new process executes, and the arguments are passed in a tuple
- The join method of the process object blocks the main thread until the child process finishes executing, and the timeout parameter can specify the time-out
- Process implementation, the main process will not close until all child processes have finished executing
- Like the Java multithreading implementation, inherit the process class, rewrite the Run method, and then instantiate, and then drop the Start method
from multiprocessing import Processimport osimport timedef test(arg): print(arg) time.sleep(4) print("the process %s is executing "%os.getpid())p1 = Process(target=test, args=("haha",)) # 以元组的形式传递参数p1.start()p1.join(timeout=2) # 子进程执行结束之后主进程才继续往下执行# timeout设置超时时间 超过这个时间如果子进程还没结束 主进程将继续执行# p1.terminate() # 杀死p1进程print("finish") # 主进程执行完之后并不会关闭 而是会等子进程执行结束再关闭# 类似Java多线程 继承Process类 重写run方法 实例化 start调用class MyProcess(Process): def run(self): time.sleep(3) print("haha...")m1 = MyProcess()m1.start()print("主进程执行到这里了....")
Process of Python multi-processes