我只是玩弄多线程,但我似乎无法得到它的工作。我看过其他问题,但没有一个真正帮助我。这里是我到目前为止的代码:Python 3.4中的多线程如何工作?
import threading, time
def hello():
for i in range(1,5):
time.sleep(1)
print("Hello")
def world():
for i in range(1,5):
time.sleep(1)
print("World")
newthread = threading.Thread(hello())
newthread.daemon = True
newthread.start()
otherthread = threading.Thread(world())
otherthread.daemon = True
otherthread.start()
print("end")
我希望得到的东西,如:
Hello
World
Hello
World
Hello
World
Hello
World
end
而是我得到:
Hello
Hello
Hello
Hello
World
World
World
World
end
感谢您的回答,完美工作,并且在启动线程之间添加了一小段延迟,以使输出正确。 :) –