0
我在尝试多线程处理我的Python应用程序。这是我怎么想的应用程序将工作:Python:返回线程结果
- 由用户
- 对于每个IPv4地址创建的IPv4地址的列表,应用程序建立在SSH连接和日志这部分将来自多线程中受益因为每个设备需要大约10秒才能完成。 ssh位全部由我的ConfDumper类处理。
- 在每个线程中,一部分数据从网络设备中提取并返回到主线程(其中有一个设备列表)
- 一旦完成所有线程,就会显示一个结果。
作为新的Python和具有多线程没有经验,我已经试过这样的事情:
import threading
import confDumper
class MyThread (threading.Thread):
device = None
# A device object is sent as agument
def __init__(self, device):
threading.Thread.__init__(self)
self.device = device
def run(self):
print "Starting scan..."
self.sshscan()
print "Exiting thread"
def sshscan(self):
s = confDumper.ConfDumper(self.device.mgmt_ip, self.device.username, self.device.password, self.device.enable_password)
t = s.getConf()
if t:
# We got the conf, return it to the main thread, somehow...
看来,当我通过一个调试的代码和步骤虽然线条一个是工作,但是一旦线程关闭,线程的所有结果都将丢失。我如何将结果返回到主线程?
当你事实上意味着它们是实例属性时,请不要像你的'device'那样声明类级属性。这不是Python怎么做的,如果你碰巧把它变成一个可变的东西而不是None,它将在未来咬你。 – deets
请参阅http://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python –
@deets请详细解释一下。我有一种错觉,但你应该怎么做? – user3305609