进程间通信:
import time
from multiprocessing import Process, Queue, current_process
# 进程间通信
class WriteProcess(Process):
"""写的进程"""
def __init__(self, q, *args, **kwargs):
super().__init__(*args, **kwargs)
self.q = q
def run(self):
ls = ['qqqqqqqqq','wwwwwwwww','rrrrrrrrr']
for l in ls:
print('写的内容{}-----{}'.format(l, current_process().name))
# 写入
self.q.put(l)
time.sleep(1)
class ReadProcess(Process):
"""读的进程"""
def __init__(self, q, *args, **kwargs):
self.q = q
super().__init__(*args, **kwargs)
def run(self):
while True:
content = self.q.get()
print("读的内容{}---{}".format(content, self.name))
if __name__ == '__main__':
q = Queue()
w_process = WriteProcess(q, name='write')
w_process.start()
r_process = ReadProcess(q, name='read')
r_process.start()
w_process.join()
# r_process.join()
# 读的进程死循环,无法等待
r_process.terminate()
进程池:
import time
from multiprocessing import current_process, Pool
def run(file_name, num):
"""写入文件"""
with open(file_name, 'a+', encoding='utf-8') as f:
# 获取当前进程对象
now_process = current_process()
content = '{}---{}-----{}\n'.format(now_process.name, now_process.pid, num)
f.write(content)
print(content)
time.sleep(2)
return 'ok'
if __name__ == '__main__':
pool = Pool(2)
file_name = 'pool_text.txt'
# for i in range(3):
# # 同步添加进程
# pool.apply(run, args=(file_name, i))
for i in range(3):
# 异步添加进程
r = pool.apply_async(run, args=(file_name, i))
print(r)
# pool.starmap(run, [(file_name, 1), (file_name, 3), (file_name, 6)])
pool.close()
pool.join()
线程创建:
# 实例的方式生成线程
import os
import threading
import time
def loop():
"""新线程的执行代码"""
n = 0
while n < 3:
now_thread = threading.current_thread()
print("线程名:", now_thread.name)
print(n)
print(os.getpid())
time.sleep(1)
n += 1
def use_thread():
"""使用线程来实现"""
# 当前正在执行的线程名称
now_thread = threading.current_thread()
print("线程名:", now_thread.name)
t = threading.Thread(target=loop, name='loop_thread')
t.start()
t.join()
if __name__ == '__main__':
print(os.getpid())
use_thread()
多线程:
import threading
import time
from concurrent.futures.thread import ThreadPoolExecutor
from multiprocessing.dummy import Pool
def run(n):
"""线程要做的事情"""
time.sleep(1)
print("-------name:{}---------n:{}--".format(threading.current_thread().name, n))
def main():
"""使用传统方式"""
t1 = time.time() # 获取当前时间
for i in range(5):
run(i)
print("任务总时间:", time.time() - t1)
def main_use_thread():
"""使用多线程做
方法1:
"""
# 资源有限 假设最多跑10个线程
t1 = time.time()
ls = []
for count in range(10):
for i in range(10):
t = threading.Thread(target=run, args=(i, )) # 传入参数 是元组类型 一个参数末尾需要,
ls.append(t)
t.start()
for l in ls:
l.join()
print(time.time() - t1)
def main_use_pool():
"""
方法2:
线程池优化"""
t1 = time.time()
n = range(100)
pool = Pool(10)
pool.map(run, n)
pool.close()
pool.join()
print(time.time() - t1)
def main_use_executor():
"""
方法3:
使用ThreadPoolExecutor优化;最优方法
"""
t1 = time.time()
n = range(100)
with ThreadPoolExecutor(max_workers=100) as executor:
executor.map(run, n)
print('==================', time.time() - t1)
if __name__ == '__main__':
main_use_thread()