1

What I would like to do, is some kind of communication between two applications:

first application would run and call the second application (first application would be closed) then if the second application has finished its job I would like to sent some kind of signal to the first application and launch it again (second application would be closed this time).

The only one idea I got is to write to a file, when the second application has finished its job and check in the first application if the file exists... is there any other way to do that?

Lucas
  • 3,517
  • 13
  • 46
  • 75
  • Not specific to Python, but what about making `second` a child of `first`, then wait for `second` to complete? This would make the inter-application communication trivial. – tuscland Jan 05 '14 at 23:15
  • @tuscland Cant made that, because the `second` one is updating the first application (`first` have to be closed in this case). – Lucas Jan 05 '14 at 23:26
  • @Lucas Do you need any signaling above and beyond the knowledge that the prior app completed? – John1024 Jan 05 '14 at 23:52

1 Answers1

0

It's a little unclear what you're trying to do. Are you simply trying to chain applications together, i.e. when the first application finishes it calls the second application, and when the second finishes it calls the first, etc.? If this is true, then you simply have to have one application spawn the other and exit immediately instead of waiting. Take a look at subprocess.Popen, which lets you do this (subprocess.call always waits).

However, it sounds like maybe you want the first application to continue running, but to reset itself when the second one finishes. In this case the second application is in fact the child of the first. In the first, you can check if the second has finished after calling Popen by using poll; then, when the second has finished, have the first app respawn itself and then exit, just as described above using Popen. If you want it to work asynchronously, you can create a separate thread in the first app that calls wait, and then exits; but you have to be careful with synchronization in this case. I can't say more because I don't know what the first app is doing while the second runs.

If you want the second application to keep running but the send a signal back to the first application, which is also running, create a pipe between the two, so that when the child writes to its stdout, it's actually writing to a pipe, which the parent can read. If your parent (the first application) is able to block waiting for the second application to finish doing what it's doing, then you just do this:

p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
str = p.stdout.readline()

Then, the call to readline will block until the second program prints something out (e.g. a line just containing "done"), at which point str in the first program will contain the line printed out by the second program.

If you need for the first program to do something else at the same time, and periodically poll the pipe in a non-blocking fashion, it gets trickier. Take a look at this answer for a solution that works on both Unix and Windows:

Non-blocking read on a subprocess.PIPE in python

Community
  • 1
  • 1
Urban Vagabond
  • 7,282
  • 3
  • 28
  • 31
  • Keep in mind that the `second` application is updating the `first` one so I'd have to exit it in order to let the `second` application overrding the `first` one. Here is how would it go: open `first` app -> close -> open second app -> update the first app -> close second app -> run first app. – Lucas Jan 06 '14 at 00:15
  • I still don't completely understand. Is the first one running when the second one is running, or not? You say "close" which suggests the first app isn't running when the second one is opened, but then "update the first app" which suggests it *is* running. Can you explain exactly what the two apps are actually doing? – Urban Vagabond Jan 06 '14 at 01:47
  • BTW, you should explain by how you want it to act, not how it might act according to some description I gave you. – Urban Vagabond Jan 06 '14 at 01:48
  • No, when the first one is running the second app is closed by default, first is running the second. When the second`s function `done()` returns `true` then its supposed to open the first one back, sending an informations that the `done()` was `true` and then the first app is going to make some other job then. – Lucas Jan 06 '14 at 15:27
  • OK, is this a graphical application? Are you on Windows? You need to clarify when you say "closed" do you mean the process exists but the window is closed, or the process doesn't exist? When the second one is "done" does that mean the process exits, or the window simply closes but the process remains around? Presumably the process should exit, in which case the first process will be notified automatically. Keep in mind that if the first app's process is running, it doesn't matter whether its window is closed -- the first app can simply reopen its own window. – Urban Vagabond Jan 07 '14 at 00:30
  • It would help a lot if you posted some code, otherwise I'm just making guesses as to what you're intending to do. BTW if you don't understand what I mean by a "process", let me know and I'll explain. – Urban Vagabond Jan 07 '14 at 00:31
  • Here is a really poor snippet of the code, please read the #comments to understand each line: http://pastebin.com/raw.php?i=cbCpR2vk – Lucas Jan 07 '14 at 01:10
  • Lucas, am I correct in saying that when the second app needs to "update" first app, it quits afterwards? If so, have the first app wait for the second to finish using the wait() command on the object returned by Popen(). When the second app finishes it terminates with an exit code, which is passed back to the first app and can be used to indicate normal vs. abnormal termination. Then the first app just spawns the third app. How come this won't work? – Urban Vagabond Jan 07 '14 at 05:55
  • No, it doesnt quits after the first app update, it can be closed by the user though. – Lucas Jan 07 '14 at 06:58
  • Oh.. I have solved that on my own, didnt know I can set the environment variables right in the `subprocess.Popen` :) – Lucas Jan 07 '14 at 07:12
  • OK, glad you figured it out. BTW if you need to communicate between two running processes, one way is for the parent to create a pipe which is inherited by the child, then you can send a signal by writing info down the pipe. Arguments to `Popen` like `stdin=PIPE` make this easy to do. – Urban Vagabond Jan 07 '14 at 11:32
  • Could you please update your answer and add the example of that? Regards – Lucas Jan 07 '14 at 21:37