Search code examples
pythonlinuxdaemon

Can you use python-daemon with Python 2.4?


I'm converting my program to run as a daemon on Linux. I'd like to use the python-daemon package to save repeating the work. However, I need to support python 2.4.

The example given on the page uses the with keyword so implies python 2.5; context managers are also listed as being supported from 2.5.

Can I just call the __enter__() and __exit__() methods myself instead? Or is there more to it than that?

This question nearly answers my question, but just misses it at the last minute.


Solution

  • As far as I can tell from the source code, it should be easily possible to use python-daemon in Python 2.4. (I don't have a Python 2.4 installation around to actually try, though.) The __enter__() and __exit__() methods of DaemonContext are essentially aliases for open() and close(), so the equivalent of

    with daemon.DaemonContext():
        do_main_program()
    

    would simply be

    context = daemon.DaemonContext()
    context.open()
    try:
        do_main_program()
    finally:
        context.close()
    

    I couldn't find anything Python 2.5 specific while skimming through all of the source code. (There are a few Python 2.4 specific constructs, though, like a few decorators and reversed(), so it won't work with Python 2.3 out of the box.)