我在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...