views:

198

answers:

4

I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the python-daemon package is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I can.

+2  A: 

In Windows it's called a "service" and you could implement it pretty easily e.g. with the win32serviceutil module, part of pywin32. Unfortunately the two "mental models" -- service vs daemon -- are very different in detail, even though they serve similar purposes, and I know of no Python facade that tries to unify them into a single framework.

Alex Martelli
Ah, so you actually recommend going down the service route in Windows; I was thinking that'd be overkill... though, I suppose Windows doesn't support background processes (only hidden windows) -- correct?
nbolton
@Nick, right -- for solidity in Windows, a service is the way to go. It's not really overkill -- it's rather simple to make a Windows service, actually, just different from making a Unix daemon (and beyond the scaffolding, which is different, the body of your code can often be essentially unchanged between the two use cases -- Unix daemon and Windows service).
Alex Martelli
A: 

The reason it's unix only is that daemons are a Unix specific concept i.e a background process initiated by the os and usually running as a child of the root PID .
Windows has no direct equivalent of a unix daemon, the closest I can think of is a Windows Service. There's a program called pythonservice.exe for windows . Not sure if it's supported on all versions of python though

zebrabox
+1  A: 

In general the concept of a daemon is Unix specific, in particular expected behaviour with respect to file creation masks, process hierarchy, and signal handling.

You may find PEP 3143 useful wherein a proposed continuation of python-daemon is considered for Python 3.2, and many related daemonizing modules and implementations are discussed.

Matt Joiner
+2  A: 

Two options come to mind:

  1. Port your program into a windows service. You can probably share much of your code between the two implementations.

  2. Does your program really use any daemon functionality? If not, you rewrite it as a simple server that runs in the background, manages communications through sockets, and perform its tasks. It will probably consume more system resources than a daemon would, but it would be quote platform independent.

Adam Matan