3

Let's say I'm building a tabbed editor in Python and I want to associate it with certain file type so that double clicking files of this type opens the file in this editor.

It's easy to open separate editor instance for each file, but what's the recommended cross-platform way for opening the file in new tab of existing editor instance if it's available?

I'm considering using sockets so that first time the editor is executed it starts listening a port and publishes the port number in a known file. Next time the editor is run, it notices the port number, lets the first instance know the file to open and exits. I don't like this plan very much, because (I suppose) in Windows the users get confusing warning when the first socket is created.

Can you recommend a more straitforward way to achieve single program instance in Python?

Aivar
  • 6,814
  • 5
  • 46
  • 78
  • 1
    we use sockets for this ... it is somewhat painful ... I dont think there is a better way (we just hardcode the port) – Joran Beasley Dec 28 '15 at 19:04
  • It's hard to do this cross-platform. On linux you use [fnctl](https://docs.python.org/3/library/fcntl.html) module but I don't think that works on windows .. – wim Dec 28 '15 at 19:06
  • 1
    You can also use a named pipe - create using `os.mkfifo()`. If it already exists, it will fail and return an error, but you can always check its existence using `os.path.exists()` - To some extent, it's a duplicate of [Python: single instance of program](http://stackoverflow.com/q/380870/298054). – jweyrich Dec 28 '15 at 19:18
  • @jweyrich I dont think mkfifo is available in windows – Joran Beasley Dec 28 '15 at 19:43
  • 1
    afaik binding a (listen) port is only allowed once no matter what platform ... just hard code the port number and put it in a try except block ... in the event of except just pass the file to the existing instance with a write and exit ... like it sounds like you are – Joran Beasley Dec 28 '15 at 19:45
  • @JoranBeasley: oops, you're right. `mkfifo` is available only on Unix systems - my bad. One still can check if the application is running on Windows and call the native `CreateNamedPipe` - but it sounds too hack-ish for someone looking for a cross-platform solution though. **IMHO, the original socket alternative sounds good** - in short, you won't need to handle connections - if the port is already bound, you know a process is running - Another solution would be having a PID file and use [psutil](https://pypi.python.org/pypi/psutil) (portable) to check if a process is alive by its PID. – jweyrich Dec 28 '15 at 20:00
  • 2
    Isn't this a duplicate of http://stackoverflow.com/questions/380870/python-single-instance-of-program ? (would like to upvote the original comment, but unfortunately it contains misleading stuff...) – Karoly Horvath Dec 28 '15 at 22:29
  • Possible duplicate of [Python: single instance of program](https://stackoverflow.com/questions/380870/python-single-instance-of-program) – Armali Feb 01 '18 at 10:37

0 Answers0