我正在使用PySide在python中编写一个程序,我解析一个IP地址文件并ping它们以查看它们是否已启动,并将其显示在QTreeWidget中。我遇到的问题是程序在输出数据之前ping所有客户端,导致程序在ping时“冻结”。如何在不冻结程序的情况下ping IP地址并打印输出?
答案 0 :(得分:0)
您可以从QThread
ping您的地址,例如:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore
class PingThread(QtCore.QThread):
addressState = QtCore.pyqtSignal(bool, QtCore.QString)
def __init__ (self):
super(PingThread, self).__init__()
def start(self, addressList):
self.addressList = addressList
super(PingThread, self).start()
def run(self):
for address in self.addressList:
status = True
ping = os.popen('ping -q -c2 {0}'.format(address), 'r')
while True:
line = ping.readline()
if not line:
break
if line.find('100% packet loss') > 0:
status = False
self.addressState.emit(status, address)
你的主要小部件中需要这样的东西:
self.pingThread = PingThread()
self.pingThread.addressState.connect(self.on_pingThread_addressState)
self.pingThread.start(myListOfAddresses)
当然是一个名为on_pingThread_addressState
的广告位。