Форум сайта python.su
Всем привет!
В процессе изучения возникла задача: вывешивать на экране уведомление, если существует определенный файл, и убирать уведомление, если файл не был создан (был удален).
Пытаюсь решить так:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import commands, os, time
while True:
process_run = commands.getoutput('ps -A | grep dzen2')
file_exists = os.path.exists('mcabber.state')
if file_exists and not process_run:
print('File exists and no process. Starting...')
os.system('echo "^fg(green)PENDING ^fg(red)EVENTS" | dzen2 -p')
if file_exists and process_run:
print('File exist and process running. Do nothing')
if not file_exists and process_run:
print('File doesn`t exist and process running. Stopping...')
os.system('killall dzen2')
if not file_exists and not process_run:
print('File doesn`t exist and process doesn`t running. Do nothing')
time.sleep(2)
Офлайн
используй subprocess, запустишь дочерний процесс не блокируя текущий, прервешь дочерний когда надо.
Офлайн
Спасибо за подсказку, попробую.
Офлайн
import threading
class backgroundTask(threading.Thread):
def __init__(self):
# Do initialization what you have to do
self.check=True
def run(self):
if self.check and checkSomething():
break
time.sleep(0.1)
bg = backgroundTask()
bg.start()
print 'hello! checkSomething() is running in the background'
# do some more operations...
#....
bg.join()# you wait for background task to finish.
print 'The End...'
Офлайн
потоки тут лишние, достаточно типа такого:
p = supbrocess.Popen('./process.sh',shell=True)
while not p.poll():
time.sleep(1)
p = supbrocess.Popen('./process.sh',shell=True)
p.wait()
Офлайн
Всем спасибо за советы!
Вот этот вариант работает как надо:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess, commands, os, time
proc = 'echo "^fg(green)PENDING ^fg(red)EVENTS" | dzen2 -p'
while True:
process_run = commands.getoutput('ps -A | grep dzen2')
file_exists = os.path.exists('mcabber.state')
if file_exists and not process_run:
subprocess.Popen(proc, shell=True)
if not file_exists and process_run:
os.system('killall dzen2')
time.sleep(2)
Отредактировано (Июнь 29, 2010 17:30:43)
Офлайн