Singleton process

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon Dec 22 20:06:14 EST 2003


On Tue, Dec 23, 2003 at 12:50:40AM -0000, Cameron Laird wrote:
> Benjamin Han  <bhan at andrew.cmu.edu> wrote:
> >
> >Any more suggestion? Thanks.
> 
> Open a socket service.  I expected Andrew to write that.

mkdir has the advantage of being fairly simple, although it does feel a bit
hackish, because that's not what directories are meant to be used for.

Slightly cleaner would be to use os.open with the O_CREAT and O_EXCL flags,
I think.  I'm not sure what the portability of those flags is, though --
whereas creating directories is supported on most platforms I can think of
:)

> Here's how I think about this:  what does the OS itself manage
> as a process-referring singleton?  Socket service!  Only one
> server can be on a port at a time; that's exactly the expressed
> requirement.

Sure, but the OS also ensures that only one file/directory can be at a
particular path at any given time, so that's fine too.  Sockets do have the
advantage of being automatically cleaned up when the process exits -- sort
of.  SO_REUSEADDR is probably good enough to get the behaviour you'd want,
though.

-Andrew.






More information about the Python-list mailing list