码迷,mamicode.com
首页 > 其他好文 > 详细

problem ????????????????????????????????????????????

时间:2018-12-13 16:32:26      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:splay   pre   content   parse   tin   []   sem   new   rev   

import asyncio
from threading import Thread
import time

t=time.time()

async def inspect(i):
    print(‘parse‘, i, time.time()-t)

async def produce(i,sem):
    async with sem:
        print(‘produce:‘, i, time.time()-t)
        await asyncio.sleep(1)
        await inspect(i)

        tasks = distribute(i, sem)
        if tasks is not None:
            await asyncio.wait(tasks)
        else:
            print(‘all complete‘, time.time())
            return True

def distribute(content, sem):
    print(‘distribute:‘,content+1, time.time()-t)
    if content < 300:
        tasks = []
        tasks.append(asyncio.run_coroutine_threadsafe(produce(content+1, sem), new_loop))
        tasks.append(asyncio.run_coroutine_threadsafe(produce(content+1, sem), new_loop))
        tasks.append(asyncio.run_coroutine_threadsafe(produce(content+1, sem), new_loop))
        return tasks
    else:
        return None


def start_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

sem=asyncio.Semaphore(3)
new_loop = asyncio.new_event_loop()
th = Thread(target=start_loop, args=(new_loop,))
th.start()

asyncio.run_coroutine_threadsafe(produce(1, sem), new_loop)

  

problem ????????????????????????????????????????????

标签:splay   pre   content   parse   tin   []   sem   new   rev   

原文地址:https://www.cnblogs.com/pythonClub/p/10114042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!