方式一
# import time
# from multiprocessing import Process
#
#
# def task(name):
# print('%s is running ' % name)
# time.sleep(3)
# print('%s is done' % name)
#
#
# if __name__ == '__main__':
# p = Process(target=task, kwargs={'name': '子進程1'}) # 調用task 傳入參數name
# p.start() # 僅僅給操作系統發送信號
#
# print('主')
方式二
import time,os
from multiprocessing import Process
class MyProcess(Process):
def __init__(self, name):
super(MyProcess, self).__init__()
self.name = name
def run(self): # 固定名字run, 會被start調用
print('child <%s> is running, parent is <%s>' % (os.getpid(), os.getppid()))
time.sleep(3)
print('child <%s> is done, parent is <%s>' % (os.getpid(), os.getppid()))
if __name__ == '__main__':
p = MyProcess('子進程')
p.start()
print('主進程<%s>, pycharm<%s>' % (os.getpid(), os.getppid()))