Windows service in production?

Discussion in 'Python' started by snorble, Aug 16, 2011.

  1. snorble

    snorble Guest

    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.
     
    snorble, Aug 16, 2011
    #1
    1. Advertising

  2. snorble

    Tim Golden Guest

    On 16/08/2011 05:32, snorble wrote:
    > 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
     
    Tim Golden, Aug 16, 2011
    #2
    1. Advertising

  3. snorble

    alex23 Guest

    snorble <> wrote:
    > 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.
     
    alex23, Aug 16, 2011
    #3
  4. snorble

    aspineux Guest

    On Aug 16, 6:32 am, snorble <> wrote:
    > 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.


    >
    > 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.
     
    aspineux, Aug 16, 2011
    #4
  5. snorble

    snorble Guest

    On Aug 16, 2:52 am, Tim Golden <> wrote:
    > On 16/08/2011 05:32, snorble wrote:
    >
    > > 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


    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?
     
    snorble, Aug 16, 2011
    #5
  6. snorble

    Tim Golden Guest

    On 16/08/2011 15:46, snorble wrote:
    > 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
     
    Tim Golden, Aug 17, 2011
    #6
  7. Re: [Python] Re: Windows service in production?

    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.
     
    Chris Gonnerman, Aug 18, 2011
    #7
  8. snorble

    Grummble Guest

    On 08/16/2011 12:32 AM, snorble wrote:
    > 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.
     
    Grummble, Aug 19, 2011
    #8
  9. Re: [Python] Re: Windows service in production?

    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.
     
    Chris Gonnerman, Aug 19, 2011
    #9
  10. On 8/15/11 9:32 PM, snorble wrote:
    > 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-----
     
    Stephen Hansen, Aug 19, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2h5YW0=?=
    Replies:
    2
    Views:
    706
    =?Utf-8?B?c2h5YW0=?=
    May 22, 2006
  2. prowyh
    Replies:
    0
    Views:
    315
    prowyh
    Nov 16, 2007
  3. Matt

    Install Web Service Into Production

    Matt, Sep 7, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    95
    Jeffrey Hasan
    Sep 13, 2004
  4. WinDev
    Replies:
    3
    Views:
    116
  5. Bill Mild
    Replies:
    0
    Views:
    728
    Bill Mild
    Feb 5, 2009
Loading...

Share This Page