Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73570 > unrolled thread
| Started by | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| First post | 2014-06-25 03:57 -0700 |
| Last post | 2014-06-25 18:46 -0600 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
subprocess can not kill when app exit() 不坏阿峰 <onlydebian@gmail.com> - 2014-06-25 03:57 -0700
Re: subprocess can not kill when app exit() Jason Friedman <jsf80238@gmail.com> - 2014-06-25 18:07 -0600
Re: subprocess can not kill when app exit() Michael Torrie <torriem@gmail.com> - 2014-06-25 18:46 -0600
| From | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| Date | 2014-06-25 03:57 -0700 |
| Subject | subprocess can not kill when app exit() |
| Message-ID | <dc5464d3-1470-442f-8a2f-87daeaa79579@googlegroups.com> |
Dear all
code in below, when close the app window. the two ping process can not kill auto and keep in the windows 7 task list.
During running, ping process under Python.exe as two thread. When app exit, this two process move the system process and keep running there.
could someone help to correct it.?
Many thanks.
############################
# -*- coding: utf-8 -*-
import sys
import time
import subprocess
from threading import Thread
import re
from PyQt4.QtGui import QMainWindow, QApplication, QStandardItemModel, QStandardItem, QWidget, QVBoxLayout, QTableView
from PyQt4.QtCore import pyqtSignature, Qt, QTimer, SIGNAL, QString, QMetaObject
from Queue import Queue
try:
_fromUtf8 = QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(500, 435)
self.centralWidget = QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.verticalLayout = QVBoxLayout(self.centralWidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.tableView = QTableView(self.centralWidget)
self.tableView.setObjectName(_fromUtf8("tableView"))
self.verticalLayout.addWidget(self.tableView)
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QApplication.translate("MainWindow", "Ping Tester", None, QApplication.UnicodeUTF8))
if sys.platform.startswith('linux'):
getdata = re.compile(r"icmp_req=(\d+) ttl=(\d+) time=([\d\.]+)\sms")
pingstr = ["ping", "-n", "-i 0.2"]
filtered = "Packet filtered"
delaytime = 200
else:
getdata = re.compile(r"=([\d\.]+)ms TTL=(\d+)")
pingstr = ["ping.exe", "-t"]
timeout = "Request timed out."
delaytime = 500
try:
with open("ips.conf", "r") as f:
t_node = f.read().decode('utf-8')
if not t_node:
raise IOError
except IOError:
with open("ips.conf", "w") as f:
t_node = u"""
8.8.8.8-Google
184.22.112.34-USAHE
"""
f.write(t_node.encode('utf-8'))
node = []
for line in t_node.split('\n'):
try:
ip, desc = line.strip().split("-")
node.append((ip, desc))
except ValueError:
pass
nodecount = len(node)
class MainWindow(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.model = QStandardItemModel()
self.model.setColumnCount(6)
self.model.setRowCount(nodecount)
self.model.setHorizontalHeaderLabels(["IP", "Description", "Loss%", "CurPing", "AvgPing", "TTL"])
for i, (ip, desc) in enumerate(node):
self.setitem(i, 0, ip)
self.setitem(i, 1, desc)
self.setitem(i, 2, "")
self.setitem(i, 3, "")
self.setitem(i, 4, "")
self.setitem(i, 5, "")
self.tableView.setModel(self.model)
for i in range(len(node)):
self.tableView.setRowHeight(i, 18)
self.resizetable()
self.timer = QTimer(self)
self.connect(self.timer,
SIGNAL("timeout()"),
self.checkitems)
self.timer.start(delaytime)
def checkitems(self):
while not q.empty():
item = q.get()
self.chgtxt(*item)
q.task_done()
self.resizetable()
def resizetable(self):
self.tableView.resizeColumnsToContents()
def chgtxt(self, x, y, value):
self.model.item(x, y).setText(value)
def setitem(self, x, y, value):
self.model.setItem(x, y, QStandardItem(value))
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
q = Queue()
def pinger(i, ip, desc):
s = ""
avgping = 0
count = 0
timeoutcount = 0
ret = subprocess.Popen(pingstr + [ip],
stdout=subprocess.PIPE)
while True:
try:
s += ret.stdout.read(1)
tryfind = getdata.findall(s)
if sys.platform.startswith('linux'):
if len(tryfind) > 0:
req, ttl, crtping = tryfind[-1]
avgping += float(crtping)
count += 1
q.put((i, 3, crtping + "ms"))
q.put((i, 4, "%.2f" % (avgping * 1.0 / count) + "ms"))
q.put((i, 5, ttl))
q.put((i, 2, "%.2f" % ((int(req) - count) * 100.0 / int(req))))
s = ""
elif filtered in s:
q.put((i, 2, "Failed"))
q.put((i, 3, "Failed"))
q.put((i, 4, "Failed"))
q.put((i, 5, "Failed"))
ret.kill()
s = ""
else:
if len(tryfind) > 0:
crtping, ttl = tryfind[-1]
avgping += float(crtping)
count += 1
q.put((i, 3, crtping + "ms"))
q.put((i, 4, "%.2f" % (avgping * 1.0 / count) + "ms"))
q.put((i, 5, ttl))
q.put((i, 2, "%.2f" % (timeoutcount * 100.0 / (count + timeoutcount))))
elif timeout in s:
timeoutcount += 1
q.put((i, 2, "-"))
q.put((i, 3, "-"))
if count:
q.put((i, 5, "%.2f" % (timeoutcount * 100.0 / (count + timeoutcount))))
else:
q.put((i, 5, "-"))
s = ""
except IOError:
print s
break
def startworkers():
for i, (ip, desc) in enumerate(node):
worker = Thread(target=pinger, args=(i, ip, desc))
worker.setDaemon(True)
worker.start()
time.sleep(delaytime / 10000.0)
startthread = Thread(target=startworkers)
startthread.setDaemon(True)
startthread.start()
sys.exit(app.exec_())
#############################
[toc] | [next] | [standalone]
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2014-06-25 18:07 -0600 |
| Message-ID | <mailman.11248.1403741241.18130.python-list@python.org> |
| In reply to | #73570 |
> code in below, when close the app window. the two ping process can not kill auto and keep in the windows 7 task list. > > During running, ping process under Python.exe as two thread. When app exit, this two process move the system process and keep running there. > > could someone help to correct it.? > Many thanks. Hello, That's a lot of code to read through. I recommend you read http://www.catb.org/~esr/faqs/smart-questions.html and then re-submit. Good luck.
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2014-06-25 18:46 -0600 |
| Message-ID | <mailman.11249.1403743589.18130.python-list@python.org> |
| In reply to | #73570 |
On 06/25/2014 06:07 PM, Jason Friedman wrote: >> code in below, when close the app window. the two ping process can not kill auto and keep in the windows 7 task list. >> >> During running, ping process under Python.exe as two thread. When app exit, this two process move the system process and keep running there. >> >> could someone help to correct it.? >> Many thanks. > > Hello, > That's a lot of code to read through. > I recommend you read > http://www.catb.org/~esr/faqs/smart-questions.html and then re-submit. > Good luck. Indeed. Post the shortest possible code that illustrates your problem, then ask some specific, clear questions and I'm sure we can help you out. With the complete, minimal example (a dozen lines of code perhaps), state what you want to happen, what does happen, and any output including full traces.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web