Only one python program running (like Firefox)? Only one python program running (like Firefox)? google-chrome google-chrome

Only one python program running (like Firefox)?


The way Firefox does it is: the first instance creates a socket file (or a named pipe on Windows). This serves both as a way for the next instances of Firefox to detect and communicate with the first instance, and forward it the URL before dying. A socket file or named pipe being only accessible from processes running on the local system (as files are), no network client can have access to it. As they are files, firewalls will not block them either (it's like writing on a file).

Here is a naive implementation to illustrate my point. On first launch, the socket file lock.sock is created. Further launches of the script will detect the lock and send the URL to it:

import socketimport osSOCKET_FILENAME = 'lock.sock'def server():    print 'I\'m the server, creating the socket'    s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)    s.bind(SOCKET_FILENAME)    try:        while True:            print 'Got a URL: %s' % s.recv(65536)    except KeyboardInterrupt, exc:        print 'Quitting, removing the socket file'        s.close        os.remove(SOCKET_FILENAME)def client():    print 'I\'m the client, opening the socket'    s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)    s.connect(SOCKET_FILENAME)    s.send('http://stackoverflow.com')    s.close()def main():    if os.path.exists(SOCKET_FILENAME):        try:            client()        except (socket.error):            print "Bad socket file, program closed unexpectedly?"            os.remove(SOCKET_FILENAME)            server()    else:        server()main()

You should implement a proper protocol (send proper datagrams instead of hardcoding the length for instance), maybe using SocketServer, but this is beyond this question. The Python Socket Programming Howto might also help you. I have no Windows machine available, so I cannot confirm that it works on that platform.


You could create a data directory where you create a "locking file" once your program is running, after having checked if the file doesn't exist yet.

If it exists, you should try to communicate with the existing process, which creates a socket or a pipe or something like this and communicates its address or its path in an appropriate way.

There are many different ways to do so, depending on which platform the program runs.


While I doubt this is how Firefox / Chrome does it, it would be possible to archive your goal with out sockets and relying solely on the file system. I found it difficult to put into text, so see below for a rough flow chart on how it could be done. I would consider this approach similar to a cookie :). One last thought on this is that with this it could be possible to store workspaces or tabs across multiple sessions.

EDIT
Per a comment, environment variables are not shared between processes. All of my work thus far has been a single process calling multiple modules. Sorry for any confusion.

File System Message Relay