1

我在 Windows 上使用 PyScripter for Python 2.7.5。

这段代码应该清楚地工作。为什么不打印running... let's kill it...

import multiprocessing
import time

# bar
def bar():
    for i in range(100):
        print "Tick"
        time.sleep(1)

if __name__ == '__main__':
    # Start bar as a process
    p = multiprocessing.Process(target=bar)
    p.start()

    # Wait for 10 seconds or until process finishes
    p.join(5)

    # If thread is still active
    if p.is_alive():
        print "running... let's kill it..."

        # Terminate
        p.terminate()
        p.join()

在说 py 文件之前在 Pyscripter 中输出:

*** Remote Interpreter Reinitialized  ***
>>> 
>>> 

保存 py 文件后在 Pyscripter 中的输出:

*** Remote Interpreter Reinitialized  ***
>>> 
running... let's kill it...
>>> 

cmd.exe 中的输出:

C:\Users\User\Desktop>python test.py
Tick
Tick
Tick
Tick
Tick
running... let's kill it...
4

0 回答 0