-3

I have 2 programs that must communicate with each other. They should share state (variables, files - i don't know how to achieve this). One program should read it and react on changes - the other should write to this global state.

Using files for this purpose is not even slow, it is difficult to read a file that someone writes at the same time.

What is the best way to achieve this shared state between programs? (looking for a cross-platform solution)

ein mensch
  • 311
  • 2
  • 9
  • 1
    What language/platform are you programming in? Here's a clue: `google IPC` . – Juan Tomas Jun 30 '16 at 14:51
  • did you code those 2 programs by yourself? do you know how to extend their functionality? have you had taken a look at json or XML? – Ramzendo Jun 30 '16 at 14:52
  • I dont know **how** to share state (is there something like a global variable, should I use files for sharing state between programs, ....) – ein mensch Jun 30 '16 at 14:54
  • btw, yes I code them myself and can extend them – ein mensch Jun 30 '16 at 14:55
  • What platform and language are we talking about? As @JuanTomas said, this is done via IPC but the specifics of the API depends on language and platform – JustSid Jun 30 '16 at 15:15

2 Answers2

0

The normal way to keep two processes synchronized in state is to use IPC (InterProcess Communication). But IPC may not work here, if by "cross-platform" you mean e.g. having a python app on Linux stay synced with a native Windows app on Windows. In that case, you're pretty much stuck with REST using HTTP to serve up Json, XML, etc.

Writing the state to a network-accessible temp file will create all kinds of problems. Better is to have one process act as a "server", serving state to "client" processes. When a client wants to update its state, it requests the latest state from the server at that time. The server responds with a Json or equivalent object that contains the latest state. The transaction is discrete. There's no chance of trying to read a file while another process is writing it, or reading a file that's out of date.

Juan Tomas
  • 4,905
  • 3
  • 14
  • 19
  • So I have to set up a server at one side and connect to it from the other. – ein mensch Jun 30 '16 at 15:41
  • "Set up a server" could mean a lot of things, most of which are not what I'm talking about. The idea is to use an HTTP library in each of your programs, so they can communicate via HTTP. One program maintains the master state and listens for HTTP requests. The other makes an HTTP request whenever it wants to update its state. You don't need to install a separate web server or anything. Just let your programs speak HTTP to one another (preferably on a port other than 80, which is used by regular web servers). – Juan Tomas Jun 30 '16 at 15:49
-1

One idea to achieve this is to export your data (variables, state, etc) in a json file and store it in a temporary file. Once you have done this, with your second program you can read that json file that has been generated by the first program and parse it so you can assign those variables to your second program.

If you are working with those programs in a network, then I would suggest to use REST.

XML and Json are formats to exchange information across platforms. You will have to make sure you have a Json parser in both programs.

I hope that helps.

Ramzendo
  • 576
  • 1
  • 5
  • 16
  • yes it helped! the problem is, i can not let the first programm write the file and then let the second read, cause they run concurrently. – ein mensch Jun 30 '16 at 15:31
  • So one program must read **while** the other writes. Is this possible with files? – ein mensch Jun 30 '16 at 15:32