[python-win32] Re: Windows service features and Python

Imre András Róbert beast at ludens.elte.hu
Mon Aug 9 04:24:16 CEST 2004


Hello Mark,

Friday, August 6, 2004, 2:16:06 AM, you wrote:

>> > I'm transforming some Python code into a Windows
>> > service. I started with pywin32-202.win32-py2.2.exe,
>> > and investigated the demo pipeTestService. This
>> > worked fine, so I started converting my code, with
>> > no paticular Windows system programming experience.

> I'm afraid your problems relate to this lack of Windows experience more than
> to Python itself.  "Python Programming on Win32" does cover alot of this
> stuff in some detail, but general overviews of Windows services would be
> useful.

I studied PPWin32 and MSDN too. Now I'm a bit brighter...


>> > Q1) How can I make my Python service support pause/
>> >     continue? Implementing SvcPause() and SvcContinue()
>> >     seems to be not enough, as I get 'Could not pause
>> >     ... service on Local Computer. The service did not
>> >     return an error. ...' dialog despite that the
>> >     SvcPause() method is executed. After this dialog
>> >     I found that the service was stopped, ending within
>> >     SvcDoRun(). Please refer to the attached code.

> You need to report a status of PAUSED to the service control manager, in the
> same way that RUNNING, START_PENDING, STOP_PENDING and STOPPED are alreay
> reported.  Indeed, if your pause takes some time, you must periodically
> report PAUSE_PENDING, until you actually enter the PAUSED state.

My error was that in SvcPause I used to set PAUSE_PENDING and then
SetEvent(self.hWaitStop), and sent no periodic reports. Now I set
status to PAUSED immediately, and omitted that SetEvent. Similarly,
SvcContinue sets status to RUNNING immediately. This works.

I also realized that its me who have to manage the periodic reporting
in pending states. I haven't tried this yet, but I think I see the
picture.


> What "pause" means is up to you and your code.  One strategy would be to use
> an event, much in the same way that "stop" is handled in the samples.

>> > Q2) How can I register my own event IDs/categories? I don't
>> >     know where/how to define my own IDs/cats. The demo seems
>> >     to support two IDs, servicemanager.PYS_SERVICE_STARTED and
>> >     servicemanager.PYS_SERVICE_STOPPED.

> See PPWin32 - in a nutshell, there are a number of predefined messages, and
> unfortunately you need to look in win32\src\PythonServiceMessages.mc in the
> source distribution (or via the CVS viewer).  These .mc files are compiled
> using MSVC6 tools, so there is currently no easy to to define your own.  You
> are free to use the various "general" ones in PythonServiceMessages.mc.

Based on the downloaded source, I created a dll with MSVC, and managed
to write events with IDs from that dll. However, for custom categories
I had to create the registry keys by hand, because I found no method for
this in PyWin32.

Now I'm trying to figure out how can I make these IDs visible by
symbolic name in my Python source, like pipeTestService uses
servicemanager.pyd. I don't want to define these twice, and
keep in sync all the time...


>> > Q3) How can I add counters to the Python service? Looking at
>> >     the files perf_install.ini and perf_install.h, this
>> >     could be possible, but after install I do not see these
>> >     counters in PerfMon. I do not see the way these files
>> >     are connected to the service demo source code... Maybe
>> >     these are already installed as the readme indicates,
>> >     but where should I look?

> Sorry, but I can't recall exactly.  perfmon counters are not for the feint
> hearted.  Again, PPWin32 covers that.

PPWin32's examples contains PipeService2.py, and this matches
with its ini files, as uses IDs defined there. (PyWin32 source
example does not use any counters.) But this only works if the
service is installed with the --perfmonini switch. Once installed
this way, I can see all those counters in PerfMon.

I found this out only by looking at the source, as its help does
not mention this switch. It would also be nice if this would
support pause and continue functions.

Thanks for the help so far.

It seems that now I got all the answers, except for one new question:

Q4) I can't make my service interactive. I never get a console
    window despite that I use the --interactive flag, and I can
    see it set under the logon tab of service properties. What
    did I get wrong?

-- 
Thanks,
  András

  
> Mark.



More information about the Python-win32 mailing list