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

foreve结束

时间:2018-12-09 14:15:31      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ever   new   run   stop   cio   reading   tar   out   safe   

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结束

标签:ever   new   run   stop   cio   reading   tar   out   safe   

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

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