标签:请求 sed mon gil time img init mina open
1、由于python只能调用一个cpu来工作,故 适合用来处理IO密集型问题。
# -*-coding:utf-8-*- _auther=‘renzhoazhoa‘ import os,subprocess from multiprocessing import Process class MyProcess(Process): def __init__(self,arg1,arg2): super().__init__() self.arg1=arg1 self.arg2=arg2 def run(self): print(self.pid) print(os.getpid()) # 两种查看进程号的方法 print(self.name) if __name__ == ‘__main__‘: print(‘Father‘,os.getpid()) p1=MyProcess(1,2) p1.start() # start会自己调用process里的run方法 p2=MyProcess(3,4) p2.start()
1.2、守护进程
守护进程会随着主进程的代码执行完毕而结束,而不是随着主进程的完毕而完毕。
# -*-coding:utf-8-*- _auther=‘renzhoazhoa‘ import os,subprocess,time from multiprocessing import Process def too(): while True: time.sleep(1) print(‘I still alive‘) def two(): print(‘*‘*10) time.sleep(8) print(‘*‘*10) if __name__ == ‘__main__‘: t1=Process(target=too) t1.daemon=True t1.start() t2=Process(target=two) t2.start() time.sleep(3) t2.terminate() # 向操作系统发送终止线程的请求,由于需要响应时间线程不会立即结束,所以下一步会返回True print(t2.is_alive()) # 返回True time.sleep(1) print(t2.is_alive()) # 返回False i=0 while i<5: print(‘I am Father jincheng‘) time.sleep(2) i+=1
标签:请求 sed mon gil time img init mina open
原文地址:https://www.cnblogs.com/zhao-zhao/p/11279176.html