how to make a python windows service know it's own identity

Discussion in 'Python' started by Chris Curvey, Jan 31, 2007.

  1. Chris Curvey

    Chris Curvey Guest

    Hi all,

    I have used the win32com libraries to set up a service called
    MyService under Windows. So far, so good. Now I need to run multiple
    copies of the service on the same machine. I also have that working.
    For monitoring and logging, I'd like each instance of the service to
    know it's own identity (MyService1, MyService2, etc.)

    But I can't quite seem to grasp how to do this. In the code below,
    the command line parameter "-i" gives the service an identity, but how
    do I get the service to understand it's identity when it is started?

    Many thanks!


    class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""

    _svc_name_ = "MyService"
    _svc_display_name_ = "My Service"

    _id_ = ''

    def SvcDoRun(self):
    provider = MyServiceClass(identifier=self._id_)
    provider.start()

    # now, block until our event is set...
    win32event.WaitForSingleObject(self.stop_event,
    win32event.INFINITE)

    # __init__ and SvcStop snipped


    ###########################################################################
    if __name__ == '__main__':
    import optparse
    parser = optparse.OptionParser()
    parser.add_option("-i", "--identifier", dest="identifier")
    (opts, args) = parser.parse_args()
    if opts.number is not None:
    MyService._svc_name_ += opts.identifier
    MyService._svc_display_name_ += opts.identifier
    MyService._provider_id_ = opts.identifier

    win32serviceutil.HandleCommandLine(MyService,
    customInstallOptions="i:")
     
    Chris Curvey, Jan 31, 2007
    #1
    1. Advertising

  2. Chris Curvey

    Larry Bates Guest

    Chris Curvey wrote:
    > Hi all,
    >
    > I have used the win32com libraries to set up a service called
    > MyService under Windows. So far, so good. Now I need to run multiple
    > copies of the service on the same machine. I also have that working.
    > For monitoring and logging, I'd like each instance of the service to
    > know it's own identity (MyService1, MyService2, etc.)
    >
    > But I can't quite seem to grasp how to do this. In the code below,
    > the command line parameter "-i" gives the service an identity, but how
    > do I get the service to understand it's identity when it is started?
    >
    > Many thanks!
    >
    >
    > class MyService(win32serviceutil.ServiceFramework):
    > """NT Service."""
    >
    > _svc_name_ = "MyService"
    > _svc_display_name_ = "My Service"
    >
    > _id_ = ''
    >
    > def SvcDoRun(self):
    > provider = MyServiceClass(identifier=self._id_)
    > provider.start()
    >
    > # now, block until our event is set...
    > win32event.WaitForSingleObject(self.stop_event,
    > win32event.INFINITE)
    >
    > # __init__ and SvcStop snipped
    >
    >
    > ###########################################################################
    > if __name__ == '__main__':
    > import optparse
    > parser = optparse.OptionParser()
    > parser.add_option("-i", "--identifier", dest="identifier")
    > (opts, args) = parser.parse_args()
    > if opts.number is not None:
    > MyService._svc_name_ += opts.identifier
    > MyService._svc_display_name_ += opts.identifier
    > MyService._provider_id_ = opts.identifier
    >
    > win32serviceutil.HandleCommandLine(MyService,
    > customInstallOptions="i:")
    >


    What is your use case for this? Why not make a single server
    process multiple providers (store them in a list or other
    container)?

    -Larry Bates
     
    Larry Bates, Feb 1, 2007
    #2
    1. Advertising

  3. Chris Curvey

    Chris Curvey Guest

    On Feb 1, 2:10 pm, Larry Bates <> wrote:
    > Chris Curvey wrote:
    > > Hi all,

    >
    > > I have used the win32com libraries to set up a service called
    > > MyService under Windows. So far, so good. Now I need to run multiple
    > > copies of the service on the same machine. I also have that working.
    > > For monitoring and logging, I'd like each instance of the service to
    > > know it's own identity (MyService1, MyService2, etc.)

    >
    > > But I can't quite seem to grasp how to do this. In the code below,
    > > the command line parameter "-i" gives the service an identity, but how
    > > do I get the service to understand it's identity when it is started?

    >
    > > Many thanks!

    >
    > > class MyService(win32serviceutil.ServiceFramework):
    > > """NT Service."""

    >
    > > _svc_name_ = "MyService"
    > > _svc_display_name_ = "My Service"

    >
    > > _id_ = ''

    >
    > > def SvcDoRun(self):
    > > provider = MyServiceClass(identifier=self._id_)
    > > provider.start()

    >
    > > # now, block until our event is set...
    > > win32event.WaitForSingleObject(self.stop_event,
    > > win32event.INFINITE)

    >
    > > # __init__ and SvcStop snipped

    >
    > > ###########################################################################
    > > if __name__ == '__main__':
    > > import optparse
    > > parser = optparse.OptionParser()
    > > parser.add_option("-i", "--identifier", dest="identifier")
    > > (opts, args) = parser.parse_args()
    > > if opts.number is not None:
    > > MyService._svc_name_ += opts.identifier
    > > MyService._svc_display_name_ += opts.identifier
    > > MyService._provider_id_ = opts.identifier

    >
    > > win32serviceutil.HandleCommandLine(MyService,
    > > customInstallOptions="i:")

    >
    > What is your use case for this? Why not make a single server
    > process multiple providers (store them in a list or other
    > container)?
    >
    > -Larry Bates


    The use case is that I have a queue of jobs that need to run. My
    service connects to a central "distributor" server, which hands out
    jobs to complete. I'd like to be able to run multiple copies of the
    distributed service so that I can make the most use of each machine
    where they run. (I'm not certain of the thread safety of some of the
    libraries I'm using, so I'm leery of going the multi-threaded route)
    Anyway, when my service gets a job to process, I'd like to know which
    copy of the service is working on which job. So I want my log
    messages to look like this:

    Job 123: Host: alpha Service: MyService A
    Job 124: Host: alpha Service: MyService B
    Job 124: Host: beta Service: MyService C

    Is that clear, or have I muddied the waters?
     
    Chris Curvey, Feb 1, 2007
    #3
  4. Chris Curvey

    Chris Mellon Guest

    On 1 Feb 2007 11:24:13 -0800, Chris Curvey <> wrote:
    > On Feb 1, 2:10 pm, Larry Bates <> wrote:
    > > Chris Curvey wrote:
    > > > Hi all,

    > >
    > > > I have used the win32com libraries to set up a service called
    > > > MyService under Windows. So far, so good. Now I need to run multiple
    > > > copies of the service on the same machine. I also have that working.
    > > > For monitoring and logging, I'd like each instance of the service to
    > > > know it's own identity (MyService1, MyService2, etc.)

    > >
    > > > But I can't quite seem to grasp how to do this. In the code below,
    > > > the command line parameter "-i" gives the service an identity, but how
    > > > do I get the service to understand it's identity when it is started?

    > >
    > > > Many thanks!

    > >
    > > > class MyService(win32serviceutil.ServiceFramework):
    > > > """NT Service."""

    > >
    > > > _svc_name_ = "MyService"
    > > > _svc_display_name_ = "My Service"

    > >
    > > > _id_ = ''

    > >
    > > > def SvcDoRun(self):
    > > > provider = MyServiceClass(identifier=self._id_)
    > > > provider.start()

    > >
    > > > # now, block until our event is set...
    > > > win32event.WaitForSingleObject(self.stop_event,
    > > > win32event.INFINITE)

    > >
    > > > # __init__ and SvcStop snipped

    > >
    > > > ###########################################################################
    > > > if __name__ == '__main__':
    > > > import optparse
    > > > parser = optparse.OptionParser()
    > > > parser.add_option("-i", "--identifier", dest="identifier")
    > > > (opts, args) = parser.parse_args()
    > > > if opts.number is not None:
    > > > MyService._svc_name_ += opts.identifier
    > > > MyService._svc_display_name_ += opts.identifier
    > > > MyService._provider_id_ = opts.identifier

    > >
    > > > win32serviceutil.HandleCommandLine(MyService,
    > > > customInstallOptions="i:")

    > >
    > > What is your use case for this? Why not make a single server
    > > process multiple providers (store them in a list or other
    > > container)?
    > >
    > > -Larry Bates

    >
    > The use case is that I have a queue of jobs that need to run. My
    > service connects to a central "distributor" server, which hands out
    > jobs to complete. I'd like to be able to run multiple copies of the
    > distributed service so that I can make the most use of each machine
    > where they run. (I'm not certain of the thread safety of some of the
    > libraries I'm using, so I'm leery of going the multi-threaded route)
    > Anyway, when my service gets a job to process, I'd like to know which
    > copy of the service is working on which job. So I want my log
    > messages to look like this:
    >
    > Job 123: Host: alpha Service: MyService A
    > Job 124: Host: alpha Service: MyService B
    > Job 124: Host: beta Service: MyService C
    >
    > Is that clear, or have I muddied the waters?
    >


    Windows services are identified by name, so if you're running multiple
    services they will need to have unique _svc_name members.
     
    Chris Mellon, Feb 1, 2007
    #4
  5. Chris Curvey

    Larry Bates Guest

    Chris Curvey wrote:
    > On Feb 1, 2:10 pm, Larry Bates <> wrote:
    >> Chris Curvey wrote:
    >>> Hi all,
    >>> I have used the win32com libraries to set up a service called
    >>> MyService under Windows. So far, so good. Now I need to run multiple
    >>> copies of the service on the same machine. I also have that working.
    >>> For monitoring and logging, I'd like each instance of the service to
    >>> know it's own identity (MyService1, MyService2, etc.)
    >>> But I can't quite seem to grasp how to do this. In the code below,
    >>> the command line parameter "-i" gives the service an identity, but how
    >>> do I get the service to understand it's identity when it is started?
    >>> Many thanks!
    >>> class MyService(win32serviceutil.ServiceFramework):
    >>> """NT Service."""
    >>> _svc_name_ = "MyService"
    >>> _svc_display_name_ = "My Service"
    >>> _id_ = ''
    >>> def SvcDoRun(self):
    >>> provider = MyServiceClass(identifier=self._id_)
    >>> provider.start()
    >>> # now, block until our event is set...
    >>> win32event.WaitForSingleObject(self.stop_event,
    >>> win32event.INFINITE)
    >>> # __init__ and SvcStop snipped
    >>> ###########################################################################
    >>> if __name__ == '__main__':
    >>> import optparse
    >>> parser = optparse.OptionParser()
    >>> parser.add_option("-i", "--identifier", dest="identifier")
    >>> (opts, args) = parser.parse_args()
    >>> if opts.number is not None:
    >>> MyService._svc_name_ += opts.identifier
    >>> MyService._svc_display_name_ += opts.identifier
    >>> MyService._provider_id_ = opts.identifier
    >>> win32serviceutil.HandleCommandLine(MyService,
    >>> customInstallOptions="i:")

    >> What is your use case for this? Why not make a single server
    >> process multiple providers (store them in a list or other
    >> container)?
    >>
    >> -Larry Bates

    >
    > The use case is that I have a queue of jobs that need to run. My
    > service connects to a central "distributor" server, which hands out
    > jobs to complete. I'd like to be able to run multiple copies of the
    > distributed service so that I can make the most use of each machine
    > where they run. (I'm not certain of the thread safety of some of the
    > libraries I'm using, so I'm leery of going the multi-threaded route)
    > Anyway, when my service gets a job to process, I'd like to know which
    > copy of the service is working on which job. So I want my log
    > messages to look like this:
    >
    > Job 123: Host: alpha Service: MyService A
    > Job 124: Host: alpha Service: MyService B
    > Job 124: Host: beta Service: MyService C
    >
    > Is that clear, or have I muddied the waters?
    >

    Ok, makes more sense now. You will be running each service on a different
    computer. If I'm understanding all you need is to supply an attribute
    to the service that represents its internal name (the one you will use
    to do the logging). If I wanted to do this I would create a .INI file
    and read it (using ConfigParser) during service class __init__ method.
    Assign self.servicename=<value from .INI file>. Then use self.servicename
    to do my logging. I have a service somewhat like you describe and this
    works fine for me.

    -Larry
     
    Larry Bates, Feb 1, 2007
    #5
    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. Giovanni Bassi
    Replies:
    0
    Views:
    673
    Giovanni Bassi
    Oct 20, 2003
  2. nalbayo
    Replies:
    2
    Views:
    5,595
    Bruce Barker
    Nov 11, 2005
  3. JimLad
    Replies:
    0
    Views:
    468
    JimLad
    Jan 16, 2009
  4. Frederick D'hont
    Replies:
    0
    Views:
    340
    Frederick D'hont
    Jul 25, 2005
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page