开启进程俩种方式
#!/usr/bin/env python
# encoding: utf-8
# Date: 2018/6/16
# # 方式一:
# from multiprocessing import Process
# import time
#
#
# # 定义task,开启进程就是执行任务
# def task(name):
# print('%s is running' % name)
# time.sleep(3)
# print('%s is done' % name)
#
#
# if __name__ == '__main__':
# # Process(target=task, kwargs={'name': '子进程1'}) # 传参数方式,一般使用下面一种
# p = Process(target=task, args=('子进程1',)) # target=task()加括号代表立刻执行
# p.start() # 仅仅只是给操作系统发送了一个信号
#
# print('主')
#
# # 主
# # 子进程1 is running
# # 子进程1 is done
# 方式二
from multiprocessing import Process
import time
class MyProcess(Process):
def __init__(self, name):
super().__init__()
self.name = name
def run(self): # 自己定义的类名称必须是run ,不能使用start
print('%s is running' % self.name)
time.sleep(3)
print('%s is done' % self.name)
if __name__ == '__main__':
p = MyProcess('子进程1')
p.start() # 调用的是类中的run
# 子进程1 is running
# 子进程1 is done