[python-win32] Re: how to run python in background on Windows ?

Michael Li mli at deform.com
Fri Jun 4 13:54:41 EDT 2004


Hi, Larry:
Thank you very much.
My python scripts not only run on Windows, but also
run on Unix. If I run my python scripts in DOS window,
I don't have any problems. I can leave the DOS window
open 24x7, but this is not the right way. I want my python
program running in background 24x7.
If I run it in service mode, I got problems.
The problems are :
1. I can not run my python program as 24X7 after reboot/login/
    logout.
2. My python program behaves differently between running in DOS window
    and running in service mode.

I use Windows XP Home Edition. I don't know this matters or not.
But I have a pure Windows program running 24x7 in background.

Here is the details of my problem(my reply to Jim Vickroy's post).
------------begin here--------------------------------------
Hi, Jim:

Thank you very much.
My scripts are little bit longer, it's not easy to post here.
I just want to put this into background to run 24x7 :
python myscript.py

In Unix, it's very easy:
% python myscript.py &

In Windows, I wrote myinstall.cpp, myrun.cpp.
myrun.cpp is a small program just following the pattern of
Windows service program.
"myinstall" will install the "myrun.exe" service into Windows service
control manager. From Control Panel->Service, when I click
start button of myservice, it will start python.
I can see python is running from Task Manager.
When I log out and log in again, the python is gone.
I can see myrun.exe is still there from Task Manager.

I use Windows XP Home Edition.
Does this matter ?
I have a pure Windows program running as a service without
any problems.

Also I got two strange thing:
1. I have to use my login account to start python.
    After installation, I saw that "Log On" of my service
    is "Local System account". After reboot the machine,
    I can see "myrun.exe", but no python started.
    I have to change to my login account with login name/password
    in the properties of my service in Control panel.
    Then I reboot my machine, no python is running, I
    have a client program in another computer to check it.
    After I log into my account, I saw python is running.
    But my pure Windows program runs well without login.
    After login, I saw that the user name of my pure Windows program
    is "SYSTEM".

2. In my python scripts, I have to check whether a file exists or not.
    I use "nRetCode = os.access(file_name, F_OK)" to check it.
    When the file is in local harddisk, I don't have any problems.
    But when the file is in mapped harddisk, I got problem.
    Let's say that my file is C:\temp\t.txt.
    No matter I run python in DOS window or in service, it always
    return the correct value, that is, "nRetCode" is True.
    When the file is X:\temp\t.txt, where X is mapped drive.
    If I run python in DOS window, it returns the correct value.
    If I run python in service mode(background), it returns
    the wrong value, that is, "nRetCode" is False.
    I believe that this is related to Windows security check, but
    I dont know how to fix it. Do you have any idea ?

How do you run your python scripts in background ?
-------------------end here--------------------------------------




Larry Bates wrote:
>>[snip]
>>Is there anyone there knowing how to run python in background as Windows' 
>>service ?
> 
> 
> You can write an NT Service that runs all the time in the
> background and sleeps for predetermined intervals then
> wakes up and does something.  This program never exits
> unless you stop the service.  This is good for things 
> that need to be running all the time to take action when
> something happens.
> 
> Services are somewhat hard to write, but are more efficient if
> startup cost of the program is high and/or the times 
> between when you wish to check are short.  An alternative
> method would be to schedule jobs in the Task Scheduler.
> If you schedule jobs to close together, you can run into 
> the problem of the first job not completing before the 
> second one starts.  NT services run to completion, then 
> sleeps for a predetermined number of milliseconds so 
> overlap is not a problem and the sleep mechanism doesn't
> tie up the CPU.
> 
> If you want to learn about writing Python services you 
> should get a copy of Mark Hammond's Python Programming
> on Win32.  It has several examples that helped me a lot.
> 
> HTH,
> Larry Bates
> Syscon, Inc.
> 
> 
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32




More information about the Python-win32 mailing list