weiter unten seht ihr mein erstes Programm.
Mein Problem ist folgendes:
In einer Endlosschleife rechne ich über eine gewisse Zeit(10min) meine CPU Load aus und möchte sie mir dann ab einem gewissen Wert (z.B 50%) per Message Dialog ausgeben lassen(so eine Art Alarm).
Das ganze funktionert auch alles ohne Probleme bis auf den MessageDialog der sich erst komplett schließt wenn der Timer(time.sleep()) abgelaufen ist.
Nun meine Frage:
1. Wie kann ich es lösen, dass der Dialog zu geht und dann erst die Zeit läuft
Anbei mein Code der nicht optimal ist woran aber noch gearbeitet wird.
Code: Alles auswählen
#!/usr/bin/python
import gtk
import os
import time
import string
strCommand = "/bin/grep \"^cpu\ \" /proc/stat | cut -b 6-"
intTime = 10
fltCPUIdleAlt = 0
fltCPUSumAlt = 0
messageLoad =""
while True:
strCPUCommand = os.popen(strCommand).readline()
fltCPUValue = [float(s) for s in strCPUCommand.split()]
print fltCPUValue
fltCPUSum = fltCPUValue[0] + fltCPUValue[1] + fltCPUValue[2] + fltCPUValue[3]
fltCPUIdle = fltCPUValue[3]
print fltCPUIdle
print fltCPUSum
if fltCPUIdleAlt >= 0:
fltCPULoad = 100 -(100* (fltCPUIdle - fltCPUIdleAlt) / (fltCPUSum - fltCPUSumAlt))
print fltCPULoad
fltCPUIdleAlt = fltCPUIdle
fltCPUSumAlt = fltCPUSum
messageLoad = "CPU Last: " + str(int(fltCPULoad))
dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
gtk.BUTTONS_YES_NO, messageLoad)
dialog.set_title("last")
respons = dialog.run()
if respons == gtk.RESPONSE_NO:
break
if respons == gtk.RESPONSE_YES:
dialog.destroy()
# hier schließt er nicht erst die MessageBox und macht dann weiter
# sondern erst nach dem sleep fertig ist
time.sleep(intTime)