foreve结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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' ) |