立即学习:https://edu.csdn.net/course/play/24458/296424?utm_source=blogtoedu
#方式一:使用python内置模块multiprocessing下的process类
from multiprocessing import Process
import time
#定义进程函数
def task(name):
print('%s is running!'%name)
time.sleep(3)
print('%s is done!'%name)
if __name__ == '__main__':
#调用process类,并且实例化
# p = Process(target=task,kwargs={'name','子进程1'})
p = Process(target=task,args=('子进程1',))
p.start()
print('主')
#方式二:自定义一个类,继承于Process类下面,便于定义自己想要的方法
from multiprocessing import Process
import time
class myprocess(Process):
#扩展父类方法
def __init__(self,name):
self.name = name#扩展的父类方法
super().__init__() # 继承父类方法
#创建运行函数run
def run(self):
#类实例化
print('%s is running!' % self.name)
time.sleep(3)
print('%s is done!' % self.name)
if __name__ == '__main__':
p = myprocess('子进程1')
p.start()
print('主进程')