只有一个线程正在启动python

2024-04-25 18:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

def pingGetterLoop():
    while(1):
        pingGetter()

def mainLoop():
    root.mainloop()


print("thread two")
threadTwo = Thread(target = mainLoop())
print("thread one")
threadOne = Thread(target = pingGetterLoop())

threadOne.start()
threadTwo.start()

由于某些原因,threadTwo从不启动,输出总是threadOne,但是当我将thread2的位置切换到threadOne上时,threadOne就不运行了。我想这是他们排队的方式,但是,我不知道怎么解决。在


Tags: targetdefrootthreadstartprinttwowhile
1条回答
网友
1楼 · 发布于 2024-04-25 18:26:37

问题是如何将函数传递给线程。你打电话给他们,而不是传召。您可以通过删除方括号()来解决此问题:

print("thread two")
threadTwo = Thread(target=mainLoop)
print("thread one")
threadOne = Thread(target=pingGetterLoop)

由于这两个函数都包含一个无休止的循环,因此调用第一个函数是永远都不会结束的。在

相关问题 更多 >