473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Python script running as Windows service: Clean shutdown

Hello,

I'm trying to run a Python script as a Windows service with a defined
shutdown. The script (enigma-client.py) handles the communications with
the server in a distributed computing effort and calls a C program
(enigma.exe) to do the computations.

enigma.exe should save its current state when receiving SIGINT or SIGTERM.
This (obviously) works under Unix and also when running the script from the
Windows command line and terminating it with Ctrl-C.

I understand that for a clean shutdown of a Windows service one would
have to use the win32 extensions and have the working thread check
for the shutdown event in short intervals (<dd**********@online.de>).
This would leave me with these options:

a) Write enigma-client.py as a service. Write enigma.exe as a
service and have it poll regularly for shutdown events.

b) Have enigma.exe save its state regularly, use srvany.exe
and forget about a graceful shutdown.
I'm new to Windows services, so I'd be grateful for corrections or
better solutions. Here is relevant part of the code (The whole thing
is at http://www.bytereef.org/enigma-client.txt ):

""" main """
cmdline = 'enigma.exe -R 00trigr.naval 00bigr.naval 00ciphertext > NUL'
eclient = Eclient()

if len(sys.argv) != 3:
eclient.usage()

if os.path.isfile(LOCKFILE):
print "enigma-client: error: found lockfile %s. \n" \
"Check that no other enigma-client process is using this directory." \
% LOCKFILE
sys.exit(1)

atexit.register(eclient.rm_lockfile)
eclient.touch_lockfile()

win32process.SetPriorityClass(
win32process.GetCurrentProcess(),
win32process.IDLE_PRIORITY_CLASS
)

while 1:
retval = os.system(cmdline) >> 8
if retval == 0:
eclient.submit_chunk(sys.argv[1], int(sys.argv[2]))
eclient.get_chunk(sys.argv[1], int(sys.argv[2]))
elif retval == 1:
eclient.get_chunk(sys.argv[1], int(sys.argv[2]))
time.sleep(10)
else:
"""./enigma has caught a signal"""
sys.exit(retval)

Stefan Krah

--
Break original Enigma messages: http://www.bytereef.org/m4_project.html
Jan 11 '06 #1
0 2101

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Fraser | last post by:
Hi We are trying to debug a problem with services created using py2exe. It seems that these problems have arisen after services were installed and removed a few times. OK, first the actual...
6
by: EW | last post by:
I have a problem when using the python script found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/360649 It is a script to remotely shutdown a windows computer. When I use it,...
8
by: Jonathan Heath | last post by:
Hi all, I have created an ASP script that enters data into an Access Database. My problem is that I'd like this script to run when the computer is shutdown or the user logs off. I think...
2
by: Mr Newbie | last post by:
I have an annoying teenager who wont go to bed because he is allways on his computer. I want to write a windows service which will shut down his computer at night and not allow this to be...
3
by: zxo102 | last post by:
Hi there, I have a python application (many python scripts) and I start the application like this python myServer.py start in window. It is running in dos window. Now I would like to put it...
16
by: diffuser78 | last post by:
I want to write a python program and call OS specific commands in it. So basically, instead of typing in on the command line argument I want to have it in a python program and let it do the action....
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
1
by: Aspersieman | last post by:
Hi All I have a windows service (attached file). I basically just calls another script every 60 seconds. I can install, start and stop this service as expected with: ParseMailboxService.py...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.