多进程以及join的用法

# # ************************************************
# # 创建多进程方式
# import os
# from multiprocessing import Process
#
# def task():
# print('this is sub process')
# print(f'sub process id {os.getpid()}')
# print(f'my id {os.getpid()} ppid is {os.getppid()}')
#
#
# print(os.getpid(),__name__)
#
# if __name__ =='__main__':
# # 注意开启进程的代码必须放在__main__下面
# # 实例化一个进程对象,让他开启进程
# p = Process(target=task)
# p.start()
# print('this is parent process')
# print(f'parent process is:{os.getpid()}')
# print('over')
#
# """
# 4280 __main__
# this is parent process
# parent process is:4280
# over
# 5576 __mp_main__
# this is sub process
# sub process id 5576
# my id 5576 ppid is 4280
# """
# # ************************************************
# from multiprocessing import Process
# import os
#
# class Downloarder(Process):
# # def __init__(self,url,size,name):
# # super().__init()
# # self.url = url
# # self.size = size
# # self.name = name
#
#
# def run(self):
# print(os.getpid())
# pass
#
#
# if __name__ == '__main__':
# m = Downloarder()
# m.start()
# m.run()
# print('parent over',os.getpid())
#
# """
# 1648
# parent over 1648
# 5684
#
# """
# ************************************************
# from multiprocessing import Process
# import os,time
#
# a = 257
#
# def task():
# global a
# print("2",a,id(a))
# a = 200
#
# if __name__ == '__main__':
# p = Process(target=task)
#
# p.start()
#
# time.sleep(5)
# print(a)
#
# """
# 2 257 2903098186800
# 257
# """
# ************************************************
# from multiprocessing import Process
# import os,time
#
# a = 257
#
# def task():
# while True:
# print("子")
#
# if __name__ == '__main__':
#
#
# p = Process(target=task)
# p.start()
# while True:
# print('主')
#
# time.sleep(4)
# print(a)
#
# """
# 主
# 子
# 子
# 子
# 子
# 子
# ......(子)
#
# """
# ************************************************
# 5. join函数

# from multiprocessing import Process
# import os,time
#
# def task():
# for i in range(100):
# print('子 run')
#
# if __name__ == '__main__':
# p = Process(target=task)
# p.start()# 向操作系统发送指令
# p.join()# 让主进程等待子进程执行完毕再继续执行
#
# print('over')
#
#
# """
# 有p.join()
# 子 run
# ...(97个)...
# 子 run
# 子 run
# over
#
#
# 没有p.join()
# over
# 子 run
# 子 run
# 子 run
# .......(97个)
#
# """
# ************************************************
# from multiprocessing import Process
# import os,time
#
# def task1(time):
# for i in range(1000):
# print(f'{name} run')
#
# def task2(time):
# for i in range(1000):
# print(f'{name} run')
#
# if __name__ == '__main__':
# p1 = Process(target=task1,args=("p1",))
# p1.start()
# p2 = Process(target=task2,args=("p2",))
# p2.start()
#
# p2.join() # 让主进程等待子进程执行完毕再执行
# p1.join()
#
#
# # 达到的效果是p1 ,p2两个程序是并发执行的,并且over一定是在所有程序执行完之后才打印的
# print('over')
#
#
# ************************************************

# join的使用
# import os,time
# from multiprocessing import Process
#
# def task(name):
# for i in range(4):
# print(f'{name} run')
#
# if __name__ == '__main__': # args是给子进程传递的参数,必须是元祖
#
#
# ps = []
# for i in range(4):
# p = Process(target=task,args=(i,))
# p.start()
# ps.append(p)
#
# # 挨个join以下
# for i in ps:
# i.join()
#
# print('over')
#
#
# """
# 0 run
# 0 run
# 0 run
# 0 run
# 1 run
# 1 run
# 1 run
# 1 run
# 2 run
# 2 run
# 2 run
# 2 run
# 3 run
# 3 run
# 3 run
# 3 run
# over
# """

# ************************************************
from multiprocessing import Process
import os,time

def task():
while True:
print('子 run',os.getpid())
# os.getpid()本身的ID os.getppid()父类的ID


if __name__ == '__main__':
p = Process(target=task,args="老司机进程")
p.start()
p.join()
print(p.name)

# p.daemon # 守护进程
# p.join()
# print(p.exitcode)# 获取进程的退出码---用处:设置好例如0:无误,1:账户错误,2:密码错误,3,地址错误。。。
# print(p.is_alive())# 查看进程是否存活
# print('zi',p.pid) # 获取进程ID
# p.terminate()# 终止进程 与start不同的是,不会立即终止,因为操作系统还有很多事情要做

if __name__ == '__main__':
"""
1. 创建服务器socket对象,绑定IP,端口,开始监听 主进程

2. 连接循环 主进程

3. 通讯循环 子进程 来了一个客户端就要开启一个子进程
"""
posted @ 2019-07-02 21:42  蜗牛少少  阅读(1219)  评论(0编辑  收藏  举报