作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
from threading import Thread
class MyClass:
#...
def method2(self):
while True:
try:
hashes = self.target.bssid.replace(':','') + '.pixie'
text = open(hashes).read().splitlines()
except IOError:
time.sleep(5)
continue
# function goes on ...
def method1(self):
new_thread = Thread(target=self.method2())
new_thread.setDaemon(True)
new_thread.start() # Main thread will stop there, wait until method 2
print "Its continues!" # wont show =(
# function goes on ...
这样可以吗?在 new_thread.start() 主线程等待完成后,为什么会这样?我没有在任何地方提供 new_thread.join()。
守护进程没有解决我的问题,因为我的问题是主线程在新线程启动后立即停止,而不是因为主线程执行结束。
最佳答案
如所写,对Thread
构造函数的调用是调用 self.method2
而不是引用它。将 target=self.method2()
替换为 target=self.method2
,线程将并行运行。
请注意,根据您的线程执行的操作,CPU 计算可能仍会因 the GIL 而序列化。 .
关于python - 新线程阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30701983/
嘿。本周的一个教程,其中一个问题要求通过使用其他函数 formatLine 和 formatList 创建一个函数 formatLines,以格式化行列表。 我的代码是这样的; type Line =
我是一名优秀的程序员,十分优秀!