foreve结束

import asyncio
from threading import Thread
import time

print('main start:',time.time())

async def start(i):
    print(i, time.time())
    await asyncio.sleep(i)
    b=run(i)
    if b is not None:
        await asyncio.wait(b)
    else:
        print('all complete',time.time())
        return True

def run(content):
    if content<3:
        tasks=[]
        tasks.append(start(content+1))
        tasks.append(start(content+1))
        tasks.append(start(content+1))
        return tasks
    print('end running', time.time())

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

new_loop=asyncio.new_event_loop()
t=Thread(target=start_loop,args=(new_loop,))
t.start()

a=asyncio.run_coroutine_threadsafe(start(1),new_loop)

while a._state !='FINISHED':
    time.sleep(1)
    print(a._state)

print(new_loop.is_closed(),'checking')
new_loop.stop()
new_loop._csock.send(b'\0')
print(new_loop.is_closed(),'checking')

  

posted @ 2018-12-09 11:03  CrossPython  阅读(154)  评论(0编辑  收藏  举报