# coding:utf-8
import os
import time
import multiprocessing
def work(count):
# 打印出进程ID os.getpid()
print(count, os.getpid())
# 阻塞5秒
time.sleep(5)
return 'result is %s, pid is %s' % (count, os.getpid())
if __name__ == '__main__':
#创建进程池,里面有5个进程
pool = multiprocessing.Pool(5)
results = []
for i in range(20):
result = pool.apply_async(func=work, args=(i, ))
# results.append(result)
# for res in results:
# print(res.get())
#
pool.close()
pool.join()
三、进程锁
Lock锁,锁一个放一个
# coding:utf-8
import os
import time
import multiprocessing
def work(count, lock):
# 进程池上锁
lock.acquire()
# 打印出进程ID os.getpid()
print(count, os.getpid())
# 阻塞5秒
time.sleep(1)
# 进程池解锁
lock.release()
return 'result is %s, pid is %s' % (count, os.getpid())
if __name__ == '__main__':
#创建进程池,里面有5个进程
pool = multiprocessing.Pool(5)
manger = multiprocessing.Manager()
lock = manger.Lock()
results = []
for i in range(20):
result = pool.apply_async(func=work, args=(i, lock))
# results.append(result)
# for res in results:
# print(res.get())
#
pool.close()
pool.join()
四、进程间的通信
4.1 队列的创建---multiprocessing
函数名
介绍
参数
返回值
Queue
队列的创建
mac_cout
队列对象
函数名
介绍
参数
返回值
put
信息放入队列
message
无
get
获取队列信息
无
str
4.2 多进程的总结
# coding:utf-8
import time
import json
import multiprocessing
class Work(object):
def __init__(self, q):
self.q = q
def send(self, message):
if not isinstance(message, str):
message = json.dumps(message)
self.q.put(message)
def send_all(self):
for i in range(20):
# 降数据放入队列
self.q.put(i)
time.sleep(1)
def receive(self):
while 1:
# 从队列中取数据
result = self.q.get()
try:
res = json.loads(result)
except:
res = result
print('recv is %s' % res)
if __name__ == '__main__':
# 创建队列
q = multiprocessing.Queue()
work = Work(q)
send = multiprocessing.Process(target=work.send, args=({'name': '小慕'},))
recv = multiprocessing.Process(target=work.receive)
send_all_p = multiprocessing.Process(target=work.send_all)
send_all_p.start()
send.start()
recv.start()
send_all_p.join()
# 终结接收端
recv.terminate()
五、线程
5.1 线程的创建 --- threading
方法名
说明
举例
Thread
创建线程
Thread(target,args)
5.2 线程对象的方法
方法名
说明
用法
start
启动线程
start()
join
阻塞指导线程执行结束
join(timeout=None)
getName
获取线程名字
getName()
setName
设置线程名字
setName(name)
is_alive
判断线程是否存活
is_alive()
setDaemon
守护线程
setDaemon(True)
5.3 线程的问题
通过线程执行的函数无法获取返回值
多个线程同时修改文件可能造成数据错乱
# coding:utf-8
import time
import random
import threading
lists = ['python', 'django', 'tornado',
'flask', 'bs5', 'requests', 'uvloop'
]
new_lists = []
def work():
if len(lists) == 0:
return
data = random.choice(lists)
lists.remove(data)
new_data = '%s_new' % data
new_lists.append(new_data)
time.sleep(1)
if __name__ == '__main__':
start = time.time()
print('old list len:', len(lists))
t_list = []
for i in range(len(lists)):
# 创建多线程
t = threading.Thread(target=work)
t_list.append(t)
t.start()
# 阻塞线程
for t in t_list:
t.join()
print('old list:', lists)
print('new list:', new_lists)
print('new_list len', len(new_lists))
print('time is %s' % (time.time() - start))
5.4 线程创建的总结
六、线程池的创建
线程池 --- concurrent
方法名
说明
举例
futures.ThreadPoolExecutor
创建线程池
tpool=ThreadPoolExecutor(max_workers)
方法名
说明
用法
submit
往线程池中加入任务
submit(target,args)
done
线程池中的某个线程是否完成了任务
done()
result
获取当前线程执行任务的结果
result()
# coding:utf-8
import time
import os
import threading
from concurrent.futures import ThreadPoolExecutor
# 线程锁
lock = threading.Lock()
def work(i):
# lock.acquire()
print(i, os.getpid())
time.sleep(1)
# lock.release()
return 'result %s' % i
if __name__ == '__main__':
print(os.getpid())
# 创建线程池
t = ThreadPoolExecutor(2)
result = []
for i in range(20):
# 往线程池中加入任务
t_result = t.submit(work, (i, ))
result.append(t_result)
for res in result:
print(res.result())