windows + r
命令行输入:cmd + ipconfig
然后查看IPv4的地址:192.168.1*6.1
ipconfig
from multiprocessing import Process
import os, time
def func(name):
print('进程的ID:', os.getpid())
print('父进程的ID:', os.getppid())
print('当前进程的名称:', name)
time.sleep(3)
if __name__ == '__main__':
# a
process_lst = []
for i in range(10):
p = Process(target=func, args=(f'进程的名称为:{i}',)) # 默认调用init
# 启动进程
p.start()
process_lst.append(p)
for p in process_lst:
p.join() # 阻塞主进程,等待P进程执行完后继续
print('主进程结束!')
p.join() 等待子进程结束后才进行下一步的操作
from multiprocessing import Process
import time, os
class MyProcess(Process):
def __init__(self, name): # 初始化方法, 创建MyProcess时被调用
Process.__init__(self) # 调用父类的初始化
self.name = name
# 继承式必须重写run方法
def run(self):
print('进程的ID:', os.getpid())
print('父进程的ID:', os.getppid())
print('当前进程的名称:', self.name)
time.sleep(3)
if __name__ == '__main__':
process_lst = []
for i in range(10):
p = MyProcess(f'进程的名称:{i}')
p.start()
process_lst.append(p)
for p in process_lst:
p.join()
print('主进程执行完毕')
from multiprocessing import Process, Queue
import os
import random
# 入队的进程
class WriteProcess(Process):
def __init__(self, name, q):
Process.__init__(self)
self.name = name
self.q = q
def run(self):
print(f'进程的名称:{self.name}, 进程的ID:{os.getpid()}')
# 入队操作
for i in range(1, 6):
x = random.randint(1000, 6666)
print(f'入队元素:{x}')
self.q.put(x)
print('入队执行结束')
# 出队的进程
class ReadProcess(Process):
def __init__(self, name, q):
Process.__init__(self)
self.name = name
self.q = q
def run(self):
print(f'进程的名称:{self.name}, 进程的ID:{os.getpid()}')
# 入队操作
for i in range(1, 6):
print(f'出队元素---------->{self.q.get()}') # get是队列的方法
if __name__ == '__main__':
# 主程序
queue = Queue() #
# 创建子进程的对象
write_process = WriteProcess('writer', queue)
read_process = ReadProcess('reader', queue)
# 启动进程
write_process.start()
read_process.start()
1 需要在进程的init中配置:self.p = pipe
2 数据发送使用:p.send(value)
3 数据接收使用: p.recv(value)
from multiprocessing import Process, Pipe
import os
class WriteProcess(Process):
def __init__(self, name, pipe):
Process.__init__(self)
self.name = name
self.p = pipe # pipe 是方法的局部变量,而self.p是类的实例属性,可以在类的任意方法中使用,而PIPE只能在init中使用
def run(self):
print(f'进程的名称:{self.name}, 进程的ID:{os.getpid()}')
for i in range(1, 9):
# 调用管道发送数据的方法: send
self.p.send(i)
print(f'写入进程执行完毕')
class ReadProcess(Process):
def __init__(self, name, pipe):
Process.__init__(self)
self.name = name
self.p = pipe
def run(self):
print(f'进程的名称:{self.name}')
for i in range(5):
print(f'出队元素:{self.p.recv()}') # recv()接收数据
if __name__ == '__main__':
#
p1, p2 = Pipe() # p1, p2指管道的两点,每一端对应一个进程
wb = WriteProcess('writer', p1)
rp = ReadProcess('reader', p2)
# 启动进程
wb.start()
rp.start()
1 p.apply(run, ('任务' + str(i),)) # 同步执行
2 p.apply_async(run, ('任务' + str(i),)) # 异步执行
# 函数式多进程
from multiprocessing.pool import Pool
import os, time, random
def run(name):
start = time.time() # 开始时间
print(f'任务名称:{name}, 进程的ID:{os.getpid()}')
time.sleep(random.choice([1, 2, 3, 4, 5])) # 注意choice 要有()
end = time.time()
print(f' ________________+ 任务名称:{name}, 进程的ID:{os.getpid()}, 耗时:{round(end - start, 2)}')
if __name__ == '__main__':
p = Pool(5) # 5个进程的进程池
# 5个进程,10个任务
for i in range(10):
# p.apply_async(run, ('任务' + str(i),)) # 异步执行
p.apply(run, ('任务' + str(i),)) # 同步执行
p.close()
p.join()
print('主进程结束')