博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程进程学习
阅读量:5283 次
发布时间:2019-06-14

本文共 4197 字,大约阅读时间需要 13 分钟。

# 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))

 

转载于:https://www.cnblogs.com/leiziv5/p/7244435.html

你可能感兴趣的文章
Java面向对象抽象类案例分析
查看>>
对SPI、IIC、IIS、UART、CAN、SDIO、GPIO的解释
查看>>
Thymeleaf模板格式化LocalDatetime时间格式
查看>>
庖丁解“学生信息管理系统”
查看>>
Pyltp使用
查看>>
其他ip无法访问Yii的gii,配置ip就可以
查看>>
php做的一个简易爬虫
查看>>
x的x次幂的值为10,求x的近似值
查看>>
jquery获取html元素的绝对位置和相对位置的方法
查看>>
ios中webservice报文的拼接
查看>>
Power BI 报告的评论服务支持移动设备
查看>>
ACdream 1068
查看>>
HDU 2665 Kth number
查看>>
记叙在人生路上对你影响最大的三位老师
查看>>
002.大数据第二天
查看>>
python装饰器
查看>>
树上的路径
查看>>
系统平均负载
查看>>
问题总结
查看>>
软件随笔
查看>>