import multiprocessing
import time
import os
def whoami(name):
print("I'm %s, in process %s" % (name, os.getpid()))
def loopy(name):
whoami(name)
start = 1
stop = 1000000
for num in range(start, stop):
print("\tNumber %s of %s. Honk!!!" % (num, stop))
time.sleep(1)
if __name__ == "__main__":
whoami("main")
p = multiprocessing.Process(target=loopy, args=("loopy",))
p.start()
time.sleep(5)
p.terminate()
此代码摘自Bill Lubanovic撰写的“ Introducing python” Book,第二版。根据这本书,当我运行此代码时,会发生以下结果:
I'm main, in process xxxx
I'm loopy, in process ----
Number 1 of 1000000. Honk!
Number 2 of 1000000. Honk!
Number 3 of 1000000. Honk!
Number 4 of 1000000. Honk!
Number 5 of 1000000. Honk!
但是,在我的情况下,仅打印“进程以退出代码0完成”。我想知道这段代码的错误之处。