python - 只有一个线程正在启​​动 python

标签 python multithreading python-3.x

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,但是当我将 threadTwo 的位置切换到 threadOne 上时,threadOne 不会运行。我想这就是他们进入队列的方式,但是我不知道如何解决它。

最佳答案

问题在于如何将函数传递给线程。您调用它们而不是传递可调用对象。您可以通过删除括号 () 来解决此问题:

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

由于这两个函数都包含无限循环,因此您永远无法调用第一个函数,然后它会永远循环。

关于python - 只有一个线程正在启​​动 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37844857/

相关文章:

python - python 中的异或位

c# - 在 WPF 中使用 EntityFrameWork 加载数据时显示进度条

java - 避免同时执行两次 Spring @Async 任务

python - 以印度/美国表示法转换数字以值的变化结束

linux - 如何在 linux 中为 python 3 安装 poppler?

PythonAnywhere + 虚拟环境 : "Could not find platform dependent libraries <exec_prefix>..."

python 预期: SSHing then updating the date

python - 在 Celery 任务中获取生成文件的 URl 的最佳方法是什么

c# - 为什么需要冗余锁对象?

Python - 输入验证