Discuss / Python / Windows上执行过程不合预期

Windows上执行过程不合预期

Topic source

对于这段代码

import threading
import asyncio
@asyncio.coroutine
def hello():
    print('Hello world! (%s)' % threading.currentThread())
    yield from asyncio.sleep(1)
    print('Hello again! (%s)' % threading.currentThread())

loop = asyncio.get_event_loop()
tasks = [hello(), hello()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

作者给出的运行过程是这样的

Hello world! (<_MainThread(MainThread, started 140735195337472)>) Hello world! (<_MainThread(MainThread, started 140735195337472)>) (暂停约1秒) Hello again! (<_MainThread(MainThread, started 140735195337472)>) Hello again! (<_MainThread(MainThread, started 140735195337472)>)

在我在 Windows 上运行出来的结果是这样的

(暂停约1秒)Hello world! (<_MainThread(MainThread, started 6368)>) Hello world! (<_MainThread(MainThread, started 6368)>) Hello again! (<_MainThread(MainThread, started 6368)>) Hello again! (<_MainThread(MainThread, started 6368)>)

感觉很奇怪,有谁能解释一下吗?

我的 win7 结果跟作者一样


  • 1

Reply