from collections import Iterable
from collections.abc import Iterator
# iter函数
s ='i am liuneng'print(isinstance(s, Iterable))print(isinstance(s, Iterator))
s_iter =iter(s)print(isinstance(s_iter, Iterable))print(isinstance(s_iter, Iterator))>>>TrueFalseTrueTrue
生成器
generator:一边循环一边计算下一个元素的算法
需要满座三个条件
每次调用都可以生成下一个元素
如果达到最后一个后,爆出StopIteration异常
可以被next调用
如何生成一个生成器
直接使用
如果函数中包含yield,则这个函数就叫做生成器
next调用函数,遇到yield返回
# 直接使用生成器
L =[x*x for x inrange(5)]# 放在中括号中的是列表生成器
G =(x*x for x inrange(5))# 放在小括号中是生成器print(type(L))print(type(G))>>><class'list'><class'generator'>
# for循环调用生成器deffib(max):
n, a, b =0,0,1while n <max:yield b
a, b= b, a+b
n +=1#需要注意,爆出的异常的返回值是return的值return'Done'
g = fib(5)for i inrange(6):
rst =next(g)print(rst)>>>11235---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)<ipython-input-14-e02f38f4be18>in<module>()10 g = fib(5)11for i inrange(6):--->12 rst =next(g)13print(rst)
StopIteration: Done
defsimple_corotine(a):print("-> start")
b =yield a
print("-> recived", a, b)
c =yield a + b
print("-> recived", a, b, c)
sc = simple_corotine(5)
aa =next(sc)print(aa)
bb = sc.send(6)print(bb)
cc = sc.send(7)print(cc)>>>-> start
5-> recived 5611-> recived 567
StopIteration # 如果达到最后一个后,爆出StopIteration异常
from concurrent.futures import ThreadPoolExecutor
import time
defreturn_future(msg):
time.sleep(3)return msg
# 创建一个线程池
pool = ThreadPoolExecutor(max_workers=2)# 往线程池加入2个task
f1 = pool.submit(return_future,'hello')
f2 = pool.submit(return_future,'world')print(f1.done())
time.sleep(3)print(f2.done())print(f1.result())print(f2.result())
concurrent中map函数
map(fn, *iterables, timeout=None)
跟map函数类似
函数需要异步执行
timeout: 超时时间
map跟submit使用一个就行
import time,re
import os,datetime
from concurrent import futures
data =['1','2']defwait_on(argument):print(argument)
time.sleep(2)return"ok"
ex = futures.ThreadPoolExecutor(max_workers=2)for i in ex.map(wait_on,data):print(i)
from concurrent.futures import ThreadPoolExecutor as Pool
#import requestsimport urllib
from urllib import request
URLS =['http://www.baidu.com','http://qq.com','http://sina.com']deftask(url, timeout=20):#return requests.get(url, timeout=timeout)return request.urlopen(url, timeout=timeout)
pool = Pool(max_workers=3)
results = pool.map(task, URLS)import time
time.sleep(20)for ret in results:print('%s, %s'%(ret.url,len(ret.read())))
from concurrent.futures import ThreadPoolExecutor as Pool
from concurrent.futures import as_completed
import requests
URLS =['http://qq.com','http://sina.com','http://www.baidu.com',]deftask(url, timeout=10):return requests.get(url, timeout=timeout)with Pool(max_workers=3)as executor:
future_tasks =[executor.submit(task, url)for url in URLS]for f in future_tasks:if f.running():print('%s is running'%str(f))for f in as_completed(future_tasks):try:
ret = f.done()if ret:
f_ret = f.result()print('%s, done, result: %s, %s'%(str(f), f_ret.url,len(f_ret.content)))except Exception as e:
f.cancel()print(str(e))