Windows service in production?

S

snorble

Anyone know of a Python application running as a Windows service in
production? I'm planning a network monitoring application that runs as
a service and reports back to the central server. Sort of a heartbeat
type agent to assist with "this server is down, go check on it" type
situations.

If using Visual Studio and C# is the more reliable way, then I'll go
that route. I love Python, but everything I read about Python services
seems to have workarounds ahoy for various situations (or maybe that's
just Windows services in general?). And there seem to be multiple
layers of workarounds, since it takes py2exe (or similar) and there
are numerous workarounds required there, depending on which libraries
and functionality are being used. Overall, reading about Windows
services in Python is not exactly a confidence inspiring experience.
If I knew of a reference example of something reliably running in
production, I'd feel better than copying and pasting some code from a
guy's blog.
 
T

Tim Golden

Anyone know of a Python application running as a Windows service in
production? I'm planning a network monitoring application that runs as
a service and reports back to the central server. Sort of a heartbeat
type agent to assist with "this server is down, go check on it" type
situations.

Don't know what it'll take to inspire you with confidence, but I have
several Python services running here without a hitch.
The longest have been running for about three years -- not without
a stop, since they have to be restarted for reasons external to the
service itself.

There's no py2exe involved, just the pywin32 service wrappers. (The
apps in question are also set up to run standalone for testing etc.).
They're mostly around a helpdesk system: one app ingests email requests
to the helpdesk; another post-processes call changes, currently to
send out email alerts to interested parties; another triggers alarms
on calls for various purposes, etc.

I don't claim they're the most sophisticated pieces of code on Earth,
but it doesn't sound like you're after anything spectacular either.

TJG
 
A

alex23

snorble said:
If using Visual Studio and C# is the more reliable way, then I'll go
that route. I love Python, but everything I read about Python services
seems to have workarounds ahoy for various situations (or maybe that's
just Windows services in general?).

What workarounds do you mean? Every example I've ever seen makes
direct use of the Windows modules for Python.
And there seem to be multiple
layers of workarounds, since it takes py2exe (or similar) and there
are numerous workarounds required there, depending on which libraries
and functionality are being used.

What about existing code recipes which were featured in the Python
Cookbook?

http://code.activestate.com/recipes/551780/

Not a hint of pyexe or weird hackery.
 
A

aspineux

Anyone know of a Python application running as a Windows service in
production? I'm planning a network monitoring application that runs as
a service and reports back to the central server. Sort of a heartbeat
type agent to assist with "this server is down, go check on it" type
situations.

I don't use service for my own python applications, even standalone
py2exe applications.
I use the Windows taskscheduler that start it once and try every
minute to start it again
but is is running all the time, and the the scheduler does nothing and
wait to see for the next minute.
When the python program crash then the scheduler restart it in the
minute.

look here how I do with tcpproxyreflector:
http://blog.magiksys.net/software/tcp-proxy-reflector#tcpr_service

In My MKSBackup http://blog.magiksys.net/mksbackup-quick-overview
I have a install() function that create the task in the scheduler for
XP and Windows2008

And this code, use a 2 min interval


print 'create task %s in scheduler' % (task_name, )
interval=raw_input_with_default('Enter interval in minutes',
'>', str(interval_in_minutes))
cmd='"%s" -c "%s"' % (os.path.join(install_target,
os.path.basename(sys.argv[0])), os.path.join(target, ini_file))
schtasks_cmd=[ 'SCHTASKS', '/Create', '/SC', 'MINUTE', '/MO',
interval, '/TN', task_name, '/RU', os.getenv('USERNAME'), '/TR', cmd ]
if sys.getwindowsversion()[0]>5:
# under 2008 ? require HIGHEST privilege
# schtasks_cmd.insert(2, 'HIGHEST')
# schtasks_cmd.insert(2, '/RL')
# under 2008, to force the system to ask for the password,
set empty password
i=schtasks_cmd.index('/RU')+2
schtasks_cmd.insert(i, '')
schtasks_cmd.insert(i, '/RP')
else:
pass


Mix all 3 source to get what you want.

Services are probably fine too, but this is another story.
 
S

snorble

Don't know what it'll take to inspire you with confidence, but I have
several Python services running here without a hitch.
The longest have been running for about three years -- not without
a stop, since they have to be restarted for reasons external to the
service itself.

There's no py2exe involved, just the pywin32 service wrappers. (The
apps in question are also set up to run standalone for testing etc.).
They're mostly around a helpdesk system: one app ingests email requests
to the helpdesk; another post-processes call changes, currently to
send out email alerts to interested parties; another triggers alarms
on calls for various purposes, etc.

I don't claim they're the most sophisticated pieces of code on Earth,
but it doesn't sound like you're after anything spectacular either.

TJG

Interesting. Normally I would use py2exe then do "myapp.exe -install"
to install the app as a service. How do you handle installing the
service? Also what does the service show under the properties, for the
executable? "python.exe script.py" or something else?
 
T

Tim Golden

Interesting. Normally I would use py2exe then do "myapp.exe -install"
to install the app as a service. How do you handle installing the
service? Also what does the service show under the properties, for the
executable? "python.exe script.py" or something else?

To install, I simply invoke the Command-line handler which comes with
the pywin32 service utils:

if __name__ == '__main__':
win32serviceutil.HandleCommandLine (Service)

(I imagine that's what py2exe's doing for you behind the scenes).

The executable shows as pythonservice.exe. The short and long
display names can of course be whatever you like.

TJG
 
C

Chris Gonnerman

Chiming in late here, but I've been running a very simple Python service
for some time now on a number of computers. It's my Raw Print Server,
available at
http://newcenturycomputers.net/projects/rawprintserver.html, and I have
instructions on the page for installing the Windows service version.
It's really quite simple to do in plain Python; the only reason to use
py2exe is if you don't want to install a full Python interpreter on the
computer. But since I generally do anyway, it doesn't matter to me.

-- Chris.
 
G

Grummble

Anyone know of a Python application running as a Windows service in
production? I'm planning a network monitoring application that runs as
a service and reports back to the central server. Sort of a heartbeat
type agent to assist with "this server is down, go check on it" type
situations.

I modified something similar to:
http://code.activestate.com/recipes/551780-win-services-helper/
and it was in production for several years without any issues.

Specifically it processed orders generated by a customer's MRP system,
delivered to us via LPR, then processed into a format our in house
windows hosted picking/shipping system could digest. Availability was a
*major* requirement, and it worked flawlessly.
 
C

Chris Gonnerman

Chiming in late here, but I've been running a very simple Python service
for some time now on a number of computers. It's my Raw Print Server,
available at
http://newcenturycomputers.net/projects/rawprintserver.html, and I have
instructions on the page for installing the Windows service version.
It's really quite simple to do in plain Python; the only reason to use
py2exe is if you don't want to install a full Python interpreter on the
computer. But since I generally do anyway, it doesn't matter to me.

-- Chris.
 
S

Stephen Hansen

Anyone know of a Python application running as a Windows service in
production? I'm planning a network monitoring application that runs as
a service and reports back to the central server. Sort of a heartbeat
type agent to assist with "this server is down, go check on it" type
situations.

If using Visual Studio and C# is the more reliable way, then I'll go
that route. I love Python, but everything I read about Python services
seems to have workarounds ahoy for various situations (or maybe that's
just Windows services in general?). And there seem to be multiple
layers of workarounds, since it takes py2exe (or similar) and there
are numerous workarounds required there, depending on which libraries
and functionality are being used. Overall, reading about Windows
services in Python is not exactly a confidence inspiring experience.
If I knew of a reference example of something reliably running in
production, I'd feel better than copying and pasting some code from a
guy's blog.

Belatedly: I run a few quite major windows services which are installed
at various customer sites in production, and we have no issues with it
being a windows service.

I basically only ever ran into two problems:

1. The lack of a console. Your service flat out /does not have/ a
console, which is a slightly weird state to be in: writing to sys.stdout
will fail. A print statement left in can crash things up -- even if in
third-party code.

Now, once you realize this is there, its easy to "fix". I end up
doing something like this very early on in processing:

class FakeFile:
def __init__(self, fp):
self._fp = fp
def write(self, data):
try:
self._fp.write(data)
except:
pass

# repeat with flush()

sys.stdout = FakeFile(sys.stdout)
sys.stderr = FakeFile(sys.stderr)

That way it'll run from a regular terminal fine and write out fine,
but if any stray attempts to print are left in, things will pass through
fine when its running as a service.

2. Importing modules with the same names as dlls in system32 can go
awry. I don't know if this is still there, I last touched this part of
our code a long, long, long time ago: but my service does some manual
PATH / PYTHONHOME / PYTHONPATH fiddling to take care of it. Its easy
to do.

It worked fine, and was stable and once going, everything worked fine.

Ultimately, I have since abandoned running things as a real service
directly, and wrote a "Metaservice" application we use in our company
instead. It runs as a service, and executes any random series of
programs beneath it, creating JOB's for each so any subprocesses of they
launch all get killed together cleanly, and handling dependencies via
between them through various means, and stuff like that. I just got
tired of dealing with windows stuff, so. :)

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJOTe3uAAoJEKcbwptVWx/lYOsIAKAQaLdPV8a9Xc0PWg3rl+wr
r9tPwROo/kF64jtSqPT6SfYho47HbzqEYi19fckSMTJRteraSbbdaAY301pC0Buj
4nOzw7tRC+ZCVnbXlNgnSyerSOft+DiHJsar15fdEDe48T45yvUOAPXKElI6C1ho
16K4Z9x5jXTejRmOpWSoi1eVfLyS+vsebamC1+SN4GKF+5ds9IvXbrP24E5oOKgC
eP8pVCilwEiGEuylxpg++Y9pOxYYQ8opXgZZprvWqUjCyILmPdgLip1uvLsdx2KK
Dc1C0YnF6jQzn+hnXqK7AsU18nNvBEQ3ijoVMnt0urun3+HiLaJ109syWW60+i8=
=y/7t
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top