首页 > 其他 > 详细

foreve结束

时间:2018-12-09 13:02:27      阅读:163      评论:0      收藏:0      [点我收藏+]
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‘)

  

foreve结束

原文:https://www.cnblogs.com/pythonClub/p/10090456.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!