defconsumer(): r = '' whileTrue: n = yield r ifnot n: return print('[CONSUMER] Consuming %s...' % n) r = '200 OK'
defproduce(c): c.send(None) n = 0 while n < 5: n = n + 1 print('[PRODUCER] Producing %s...' % n) r = c.send(n) print('[PRODUCER] Consumer return: %s' % r) c.close()
asyncdeffactorial(name, num): """docstring for factorial""" f = 1 for i inrange(2, num + 1): print(f"Task {name}: Compute factorial({i})...") await asyncio.sleep(1) f *= i print(f"Task {name}: factorial({num}) = {f}")
asyncdeffactorial(name, number): f = 1 for i inrange(2, number + 1): print(f"Task {name}: Compute factorial({i})...") await asyncio.sleep(1) f *= i print(f"Task {name}: factorial({number}) = {f}")
loading https://www.baidu.com/... loading https://www.sina.com/... loading https://www.qq.com/... loading https://www.bilibili.com... loading https://www.zhihu.com... Traceback (most recent call last): File "crawl_async.py", line 27, in <module> asyncio.run(main()) File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete return future.result() File "crawl_async.py", line 24, in main await asyncio.gather(*tasks) File "crawl_async.py", line 14, in load_url res = await requests.get(url,timeout=timeout) TypeError: object Response can't be used in 'await' expression
loading https://www.baidu.com/... res https://www.baidu.com/:200 loading https://www.sina.com/... res https://www.sina.com/:200 loading https://www.qq.com/... res https://www.qq.com/:200 loading https://www.bilibili.com... res https://www.bilibili.com:200 loading https://www.zhihu.com... res https://www.zhihu.com:403 spend time: 1.98 seconds.
loading https://www.baidu.com/... loading https://www.sina.com/... loading https://www.qq.com/... loading https://www.bilibili.com... loading https://www.zhihu.com... res https://www.qq.com/:200 res https://www.sina.com/:200 res https://www.baidu.com/:200 res https://www.zhihu.com:200 res https://www.bilibili.com:200 spend time: 0.46 seconds.
loading https://www.baidu.com/... loading https://www.sina.com/... loading https://www.qq.com/... loading https://www.bilibili.com... loading https://www.zhihu.com... res https://www.sina.com/:200 res https://www.baidu.com/:200 res https://www.qq.com/:200 res https://www.zhihu.com:403 res https://www.bilibili.com:200 spend time: 0.54 seconds.
defload_url(url): """docstring for load_url""" print(f"loading {url}...") with r.get(url) as conn: print(f"res {url}:{conn.status_code}")
pool = Pool(50) start = time.time() for url in URLS: pool.apply_async(load_url, args=(url,)) pool.close() pool.join() print("spend time: %.2f seconds." % (time.time() - start))
执行结果
1 2 3 4 5 6 7 8 9 10 11
loading https://www.baidu.com/... loading https://www.sina.com/... loading https://www.qq.com/... loading https://www.bilibili.com... loading https://www.zhihu.com... res https://www.sina.com/:200 res https://www.qq.com/:200 res https://www.baidu.com/:200 res https://www.zhihu.com:403 res https://www.bilibili.com:200 spend time: 1.33 seconds.