Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 code pattern for process communication
#1
many programs, especially GUI apps, when a 2nd one is started, tell the 1st one to open the window and the 2nd on exits.  i am looking for a suggested code pattern for this kind of communication setup where the process that is the 2nd instance of an executable can find and set up brief communications with the 1st instance of the same executable.  in particular, i am curious how to avoid finding a process of a different executable that is also being interpreted by python.  in a typical GUI case, this communication would send what X display the 2nd instance was started on and any specified parameters to the 1st instance.

this is not a case of existing code that fails because things have not reached that level yet, so there is no (failing) code to show.  i am still thinking through many potential projects and looking for a pattern for this.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
Sockets? Usually, they are used for interprocess comunication
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
(Sep-07-2017, 02:54 AM)wavic Wrote: Sockets? Usually, they are used for interprocess comunication
that's what i was initially thinking of doing.  but that would mean the 1st process is perpetually listening on some port.  i don't see that happening.  and i don't see a unix named socket in the file space, either.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
If you know the pid of the other process, you could use a signal.  The original process would set a signal handler, then anything that sent that signal, would trigger the event to fire off (in this case, opening a new browser window).  That's a little more lightweight than using sockets.

https://docs.python.org/3/library/signal.html
Quote
#5
Quote:That's a little more lightweight than using sockets.
It should be. Event-driven application uses less resources
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#6
(Sep-26-2017, 08:14 PM)nilamo Wrote: If you know the pid of the other process, you could use a signal.  The original process would set a signal handler, then anything that sent that signal, would trigger the event to fire off (in this case, opening a new browser window).  That's a little more lightweight than using sockets.

https://docs.python.org/3/library/signal.html
sure, you can send a signal, but that's just an event.  it would have nice to have had, from the beginning, a means to send a block of data from one process to another.  it would be like a datagram where process id == port number, and the listener can only listen on that, and fds can be passed along.  that would allow real communication.  the receiving process gets the process id of the sender, its {e,r}{g,u}id,  ppid, and other info along with up to 4k of data and up to 4 fds.  if that had been in the unix design, lots of things would be so much easier.  but maybe named sockets can be adapted to this.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#7
Well, asyncio is capable to wait for a connection and once a such is established a could be used for this purpose. It will not be high CPU load as using while loop.

I forgot to mention that the built-in asyncio Protocols or Transport must be used since there are not blockable.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#8
Unix/Linux has named pipes, which sound almost exactly like what you're talking about.  https://docs.python.org/3/library/os.html#os.mkfifo
How important is it that your program works on windows?
Quote
#9
the big issue for me is finding the other process running the same python code, and being sure the match does not get it wrong by matching "python" to "python" in the process argv[0] string, which a C program might naȉvely do.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#10
Maybe a message queue can help. I've used sometimes http://zeromq.org/bindings:python over network. But you can use this also across processes on the same machine and in the same process. There are different patterns. Asyncio is also supported.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to sharing object between multiple process from main process using Pipe Subrata 1 625 Sep-03-2019, 09:49 PM
Last Post: woooee
  Simple inter-process communication: Switching from Perl to Python Pappy 1 505 May-10-2019, 12:29 AM
Last Post: Pappy
  Process finished with exit code -107374819 (0xC0000375) mrazko 2 1,782 Apr-05-2019, 12:46 PM
Last Post: mrazko
  code pattern to test if list has all the same Skaperen 5 1,040 Jul-25-2018, 06:09 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)