# encoding: utf-8"""@author: lileilei@site: @software: PyCharm@file: login.py@time: 2017/7/26 10:28"""import requests,timefrom multiprocessing import Poolurl='http://www.jd.com'total=0suc=0fail=0ecept=0maxtime=0mintime=0gt3=0lt3=0def baiduGent(): global total global suc global fail global gt3 global lt3 global ecept try: st=time.time() conn=requests.get(url) res=conn.status_code if res==200: total+=1 suc+=1 else: total+=1 fail+=1 time_span = time.time() - st print(time_span) if time_span>3: gt3+=1 else: lt3+=1 except Exception as e: print(e) total+=1 ecept+=1if __name__ =='__main__': print('===========请求开始===========') start_time = time.time() pools = Pool(100) for i in range(10): pools.apply_async(baiduGent,args=()) pools.close() pools.join()from multiprocessing import Process,Queue import os, time, randomdef write(q): print('Process to 产生: %s' % os.getpid()) for value in ['苹果', '香蕉', '橘子']: print('产生 %s to queue...' % value) q.put(value) time.sleep(random.random())def read(q): print('Process to 消费: %s' % os.getpid()) while True: value = q.get() print('消费 %s from queue.' % value)if __name__=='__main__': q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) pw.start() pr.start() pw.join() pr.terminate()m=list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))print(m)f=list(map(lambda x:True if x%3==0 else False,range(100)))print(f)import asyncio@asyncio.coroutineasync def hello(): print ('hello word') r=await asyncio.sleep(1) print('hello again')loop=asyncio.get_event_loop()loop.run_until_complete(hello())loop.close()import threadingimport asyncio@asyncio.coroutinedef hello(): print('Hello world! (%s)' % threading.currentThread()) yield from asyncio.sleep(1) print('Hello again! (%s)' % threading.currentThread())loop = asyncio.get_event_loop()tasks = [hello(), hello()]loop.run_until_complete(asyncio.wait(tasks))loop.close()import asyncio@asyncio.coroutinedef wget(host): print('wegt %s ....'%host) connt=asyncio.open_connection(host,80) reder,writer=yield from connt hserd= 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host writer.write(hserd.encode('utf-8')) yield from writer.drain() while 1: line=yield from reder.readline() if line ==b'\r\n': break print('%s header > %s' % (host, line.decode('utf-8').rstrip())) writer.close()loop=asyncio.get_event_loop()tasks=[wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]loop.run_until_complete(asyncio.wait(tasks))loop.close()import asynciofrom aiohttp import webasync def index(request): await asyncio.sleep(0.5) return web.Response(body=b'Index
',content_type='text/html')async def hello(request): await asyncio.sleep(0.5) text = 'hello, %s!
' % request.match_info['name'] return web.Response(body=text.encode('utf-8'),content_type='text/html')async def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/hello/{name}', hello) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000) print('Server started at http://127.0.0.1:8000...') return srvloop = asyncio.get_event_loop()loop.run_until_complete(init(loop))loop.run_forever()import asyncio,time@asyncio.coroutineasync def hello(): print ('hello word')start_time=time.time()loop=asyncio.get_event_loop()tasks=[]for i in range(10000): tasks.append(hello()) loop.run_until_complete(asyncio.wait(tasks))loop.close()print('异步处理时间:%s'%(time.time()-start_time))import time,gevent def print_s(num): st=time.time() conn=requests.get('http://www.jd.com') res=conn.status_code if res==200: print('chenggong') else: print('shibai')start_time=time.time()events=[gevent.spawn(print_s,num)for num in range(10000)]gevent.joinall(events)print('协程时间:%s'%(time.time()-start_time))