我有点被难住了。这是密码。你知道吗
def getIP(self,commandline):
"""
A thread that sets an IP using the method specified in commandline
"""
getter_process = subprocess.Popen(commandline,stdout=subprocess.PIPE)
myip = getter_process.communicate()[0].decode(encoding="ascii", errors="ignore").replace("\n","")
print("getIP with ", commandline, " result:" , myip)
if isIP(myip):
self.lock1.acquire()
try:
GLib.idle_add(self.setIndicatingMenus,myip,self.hIndicator)
finally:
self.lock1.release()
print("debug2")
print("getIP end")
return False
def askIP(self,ind):
"""
Start subprocesses asking for external IP in separate threads
"""
print("askIP start")
#list of commandlines that are supposed to return IP as a result
IPgettingMethods = [
["dig", "+short", "myip.opendns.com", "@resolver1.opendns.com"],
["dig", "+short", "myip.opendns.com", "@resolver1.opendns.com"],
# and other command line commands I may want in separate threads
]
for i in IPgettingMethods:
th = Thread(target=self.getIP, args=(i,))
th.start()
return False
基本上,每当调用askIP
时,它都会启动几个getIP
线程,这些线程执行IPgettingMethods
中指定的子进程。这些子进程可能以不同的速度返回myip
值。而且,它们可能不会返回有效值(在这种情况下,isIP(myip)
将返回False
)。但是一个(或多个)线程最终将到达GLib.idle_add(self.setIndicatingMenus,myip,self.hIndicator)
。你知道吗
我的问题是:如何使每次调用askIP
时GLib.idle_add(self.setIndicatingMenus,myip,self.hIndicator)
只运行一次?换言之,我怎样才能只让最先到达它的线程调用它,而让所有其他线程忽略它?你知道吗
也许我可以在“winner”线程调用GLib.idle_add
之后杀死其他线程?你知道吗
您应该使用全局变量作为保护:到达该执行点的第一个线程将设置该变量,其他线程在看到已设置该变量时,跳过该代码段:
“手动”终止线程几乎从来都不是一个好主意。当他们到达函数的末尾时,让他们死去,通过设置一个守卫来阻止他们工作。你知道吗
这听起来很适合使用^{} 一种提交和控制多个作业的方式。在这里,您有许多助手线程来完成调用子进程的后腿工作,还有一个主线程来收集结果并决定如何处理它们。例如:
适用于您的问题:
相关问题 更多 >
编程相关推荐