13 并发编程-(线程)-异步调用与回调机制&进程池线程池小练习

#提交任务的两种方式
#1、同步调用:提交完任务后,就在原地等待任务执行完毕,拿到结果,再执行下一行代码,导致程序是串行执行

一、提交任务的两种方式

1、同步调用:提交任务后,就在原地等待任务完毕,拿到结果,再执行下一行代码,导致程序串行执行

from concurrent.futures import ThreadPoolExecutor
import time
import random
def produce(name):
print('%s is producing' %name)
time.sleep(random.randint(3,5))
res=random.randint(7,13)*'#'
return {'name':name,'res':res} def number(commodity):
name=commodity['name']
size=len(commodity['res'])
print('%s 制造了 《%s》件商品' %(name,size)) if __name__ == '__main__':
pool=ThreadPoolExecutor(13) p1=pool.submit(produce,'alex').result() # 取得返回的结果
number(p1) p2=pool.submit(produce,'wupeiqi').result()
number(p2) p3=pool.submit(produce,'yuanhao').result()
number(p3) alex is producing
alex 制造了 《12》件商品
wupeiqi is producing
wupeiqi 制造了 《12》件商品
yuanhao is producing
yuanhao 制造了 《11》件商品

2、回调函数--异步调用:提交完任务后,不用原地等待任务执行完毕,

13 并发编程-(线程)-异步调用与回调机制&进程池线程池小练习

from concurrent.futures import ThreadPoolExecutor
import time
import random
def produce(name):
print('%s is producing' %name)
time.sleep(random.randint(3,5))
res=random.randint(7,13)*'#'
return {'name':name,'res':res} def number(commodity):
commodity = commodity.result()
name=commodity['name']
size=len(commodity['res'])
print('%s 制造了 《%s》件商品' %(name,size)) if __name__ == '__main__':
pool=ThreadPoolExecutor(13)
pool.submit(produce,'alex').add_done_callback(number)
pool.submit(produce,'wupeiqi').add_done_callback(number)
pool.submit(produce,'yuanhao').add_done_callback(number) alex is producing
wupeiqi is producing
yuanhao is producing
yuanhao 制造了 《9》件商品
alex 制造了 《8》件商品
wupeiqi 制造了 《12》件商品

3、进程池线程池小练习

我们在浏览器上输入一个网址,就能看到一个页面内容,中间经历了那些过程呢:

浏览器本质套接字客户端,套接字服务端在站点,服务器主机上,把目标文件下载到本机

3.1 提取网页信息(回调函数)

# import requests,time
# requests = requests.get('https://www.cnblogs.com/foremostxl/p/9734442.html')
# print(requests.text) from concurrent.futures import ThreadPoolExecutor
import requests,time
def get(url):
request = requests.get(url)
time.sleep(3)# 模拟网络延迟
return {'url':url,'content':request.text}
def parse(res):
res = res.result()
print('%s parse res is %s' % (res['url'], len(res['content'])))
if __name__ == '__main__':
urls=[
'http://www.cnblogs.com/foremostxl',
'https://www.python.org',
'https://www.baidu.com',
]
pool = ThreadPoolExecutor(3)
for url in urls:
pool.submit(get,url).add_done_callback(parse) http://www.cnblogs.com/foremostxl parse res is 13105
https://www.baidu.com parse res is 2443
https://www.python.org parse res is 49097

3.2 基于线程池实现套接字通信

服务端:

import socket
from concurrent.futures import ThreadPoolExecutor
def Server(ip_port):
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(ip_port)
server.listen(5)
while True:
conn ,addr = server.accept()
pool.submit(communicate,conn)
server.close()
def communicate(conn):
while True:
try:
data = conn.recv(1024)
conn.send(data.upper())
except Exception as e:
print(e)
break
conn.close() if __name__ == '__main__':
ip_port = ('127.0.0.1',8080)
pool = ThreadPoolExecutor(2)
Server(ip_port)

客户端:

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip_port = ('127.0.0.1',8080)
client.connect(ip_port)
while True:
msg = input('>>').strip()
if not msg:break
client.send(msg.encode('utf-8'))
data = client.recv(1024)
print(data.decode('utf-8'))
client.close()
上一篇:Sybase配置中文语言支持及字符集


下一篇:Java语言基础——数据类型与运算符