Re: win32 service and time.sleep()

2005-09-22 Thread Steve Horsley
Oracle wrote:
 On Tue, 20 Sep 2005 10:49:13 -0400, rbt wrote:
 
 I have a win32 service written in Python. It works well. It sends a
 report of the status of the machine via email periodically. The one
 problem I have is this... while trying to send an email, the script
 loops until a send happens and then it breaks. Should it be unable to
 send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
 tries again. This is when the problem occurs. I can't stop the service
 while the program is sleeping. When I try, it just hangs until a reboot.
 Can some suggest how to fix this?

 
 You could try doing it the hard way.  In a loop, sleep for 1-5 seconds at
 a time.  Everytime you complete a sleep, check the elapsed time.  If the
 elapsed time is = the total sleep duration, fall out of the sleep loop
 and try your email again.  This should allow your service to stop within
 1-5 seconds of your request while imposing little to no load on the system.

I adopted that solution just last week. When you wake (every few 
seconds), you check for a stop flag (and exit if needed of 
course) before checking if its time to do work-work. Works a treat.

Another option is to use a threading.Condition, and have the 
service thread wait(600). The thread that calls to stop the 
service can set the stop flag and then notify() the Condition.

Steve
-- 
http://mail.python.org/mailman/listinfo/python-list


win32 service and time.sleep()

2005-09-20 Thread rbt
I have a win32 service written in Python. It works well. It sends a
report of the status of the machine via email periodically. The one
problem I have is this... while trying to send an email, the script
loops until a send happens and then it breaks. Should it be unable to
send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
tries again. This is when the problem occurs. I can't stop the service
while the program is sleeping. When I try, it just hangs until a reboot.
Can some suggest how to fix this?

Thanks,
rbt
-- 
http://mail.python.org/mailman/listinfo/python-list


win32 service and time.sleep()

2005-09-20 Thread rbt
I have a win32 service written in Python. It works well. It sends a
report of the status of the machine via email periodically. The one
problem I have is this... while trying to send an email, the script
loops until a send happens and then it breaks. Should it be unable to
send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
tries again. This is when the problem occurs. I can't stop the service
while the program is sleeping. When I try, it just hangs until a reboot.
Can some suggest how to fix this?

Thanks,
rbt

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: win32 service and time.sleep()

2005-09-20 Thread Oracle
On Tue, 20 Sep 2005 10:49:13 -0400, rbt wrote:

 I have a win32 service written in Python. It works well. It sends a
 report of the status of the machine via email periodically. The one
 problem I have is this... while trying to send an email, the script
 loops until a send happens and then it breaks. Should it be unable to
 send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
 tries again. This is when the problem occurs. I can't stop the service
 while the program is sleeping. When I try, it just hangs until a reboot.
 Can some suggest how to fix this?
 

You could try doing it the hard way.  In a loop, sleep for 1-5 seconds at
a time.  Everytime you complete a sleep, check the elapsed time.  If the
elapsed time is = the total sleep duration, fall out of the sleep loop
and try your email again.  This should allow your service to stop within
1-5 seconds of your request while imposing little to no load on the system.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: win32 service and time.sleep()

2005-09-20 Thread Laszlo Zsolt Nagy
rbt wrote:

I have a win32 service written in Python. It works well. It sends a
report of the status of the machine via email periodically. The one
problem I have is this... while trying to send an email, the script
loops until a send happens and then it breaks. Should it be unable to
send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
tries again. This is when the problem occurs. I can't stop the service
while the program is sleeping. When I try, it just hangs until a reboot.
Can some suggest how to fix this?
  

Yes. Generally, most of the win32 services work like this:

- the main thread listens to win32 service commands
- when starting the service, you should create a new worker thread that 
does the job for you
- when stopping the service, your service should report 
win32service.SERVICE_STOP_PENDING immediately, and ask the worker thread 
to terminate
- you should be continuously reporting win32service.SERVICE_STOP_PENDING 
until your workder thread has stopped

Here is a simple module that uses a 'Processor' class and a new thread 
to do the work.
(The full program is here: http://mess.hu/download/SimpleHTTPService.zip )

class Service(win32serviceutil.ServiceFramework):
_svc_name_ = SERVICE_NAME
_svc_display_name_ = SERVICE_DISPLAY
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.stopped = threading.Event()
self.stopped.clear()
self.logger = getLogger(SERVICE_NAME)

def SvcStop(self):
self.logger.info(Got SvcStop, trying to stop service...)
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.stopped.set()

def SvcDoRun(self):
Write an event log record - in debug mode we will also see 
this message printed.
try:
import servicemanager
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, '')
)
self.logger.info(Started.)
self.logger.debug(Creating processor instance)
processor = Processor(self.stopped)
self.logger.debug(Starting processor thread)
thread.start_new_thread(processor.Process,())
self.logger.debug(Waiting for the processor thread to finish)
self.stopped.wait()
self.logger.debug(Stopping)
time.sleep(1)
while not processor.stopped.isSet():

self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000)
time.sleep(5)
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, )
)
self.logger.info(Stopped)
except:
self.logger.error('',exc_info = sys.exc_info())


if __name__=='__main__':
win32serviceutil.HandleCommandLine(Service)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: win32 service and time.sleep()

2005-09-20 Thread Steve Holden
rbt wrote:
 I have a win32 service written in Python. It works well. It sends a
 report of the status of the machine via email periodically. The one
 problem I have is this... while trying to send an email, the script
 loops until a send happens and then it breaks. Should it be unable to
 send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
 tries again. This is when the problem occurs. I can't stop the service
 while the program is sleeping. When I try, it just hangs until a reboot.
 Can some suggest how to fix this?

One way would be to maintain a Queue.Queue containing the messages that 
need to be sent and, rather than sleeping to retry just retrying when 
enough time has elapsed. That way you can keep pumping messages and so 
on to your heart's content, though you will have to keep track of the 
messages still to be sent.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.pycon.org

-- 
http://mail.python.org/mailman/listinfo/python-list