• 线程进程学习


    # encoding: utf-8
    """
    @author: lileilei
    @site: 
    @software: PyCharm
    @file: login.py
    @time: 2017/7/26 10:28
    """
    import  requests,time
    from  multiprocessing import  Pool
    url='http://www.jd.com'
    total=0
    suc=0
    fail=0
    ecept=0
    maxtime=0
    mintime=0
    gt3=0
    lt3=0
    def  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+=1
    if __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, random
    def 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.coroutine
    async 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 threading
    import asyncio
    
    @asyncio.coroutine
    def 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.coroutine
    def wget(host):
        print('wegt %s ....'%host)
        connt=asyncio.open_connection(host,80)
        reder,writer=yield from connt
        hserd= 'GET / HTTP/1.0
    Host: %s
    
    ' % host
        writer.write(hserd.encode('utf-8'))
        yield from writer.drain()
        while 1:
            line=yield from reder.readline()
            if line ==b'
    ':
                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 asyncio
    from aiohttp import web
    async def index(request):
        await asyncio.sleep(0.5)
        return web.Response(body=b'<h1>Index</h1>',content_type='text/html')
    async def hello(request):
        await asyncio.sleep(0.5)
        text = '<h1>hello, %s!</h1>' % 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 srv
    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop))
    loop.run_forever()
    import asyncio,time
    @asyncio.coroutine
    async 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))
  • 相关阅读:
    LIPS的历史
    语法分析生成器 LEX
    Effective JAVA 中有关Exception的几条建议
    Code Reading chap10
    Code Reading chap8
    Code Reading chap7
    Code Reading chap11
    Code Reading chap9
    软件设计中的抽象层次
    Invalid bound statement (not found) @Update注解写的怎么还报错!
  • 原文地址:https://www.cnblogs.com/leiziv5/p/7244435.html
Copyright © 2020-2023  润新知