标签: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