from multiprocessing import Process
import os
def run(num):
print os.getpid(),os.getppid()
print num
process = Process(target=run, args=(100,), kwargs={})
process.start()
process.join()
2、通过继承Process类来创建进程类,在有类来创建实例进程对象
from multiprocessing import Process
from os import getpid
class SubProcess(Process):
def __init__(self, num):
super(SubProcess, self).__init__()
self.num = num
def run(self):
print ‘zijincheng‘, getpid()
self.work()
def work(self):
print ‘haha‘
process_object = SubProcess(100)
process_object.start()
process_object.join()
3、
import os
import time
from multiprocessing import Process
def sub_process_fun(num, a):
print os.getpid(),‘####‘, 5679 os.getppid(),‘---‘5678
for i in range(4):
pass
print os.getpid(),‘*****‘ 5678
# process_obj = Process(target=sub_process_fun, args=(100,), kwargs={"a": 10}, name="itcast")
process_obj = Process(target=sub_process_fun, args=(100,), kwargs={"a": 10})
process_obj.start()
print process_obj.name
print process_obj.pid,‘**********‘ 5679 查看子进程pid
print process_obj.is_alive()
process_obj.join()
process_obj.terminate() 停止了子进程,但是没有进行资源回收,所以下一行is_alive 返回值是true
print("父进程:子进程的状态 %s" % process_obj.is_alive())
print("父进程: 回收子进程的资源")