There are many examples of how to do this:
1) Communicate between different processes in the same program.
2) Communicate between the client / server through the network
However, this question has no good example anywhere:
- What is the canonical way to send a string from Python A to program B, which locks and processes this string and then waits for another in a loop ?
It seems to me that I have met the answer many times, but I could not create a working example.
Additional implied requirements:
- There are actually two different programs: the example should actually have two different programs (i.e. two progA.py, progB.py files that can be separately launched from the command line on two screens on the same computer) do not use any forking or multiprocess to create a client and server.
- Please suggest a way to do this, allowing you to send strings with delimiters of length to a reasonable length, rather than receiving the exact number of bytes of the correct data size. (The latter is more prone to implementation errors).
- Ideal to do this without using localhost internet connections.
For instance; when the reader uses:
pipein = open(pipe_name, 'r') while program.KeepRunning: action = pipein.readline()[:-1] program.processLine(line) time.sleep(1)
and the author uses:
command = "enable" pipeout = os.open(pipe_name, os.O_WRONLY) os.write(pipeout, command) os.write(pipeout, "\n")
as suggested at http://www.python-course.eu/pipes.php the reader gets stuck in an infinite loop by reading an empty line.
Interestingly, adding if(action == 'enable'): longFunction() to the program.processLine function causes some of the code in the longFunction execute before getting stuck, reading out empty lines forever.
On the other hand, all examples using the more modern, lower-level subprocess module include only multi-threaded applications, not several applications. Other implementations include sockets and networks.
While I was trying to use sockets, this leads to an error like “something went wrong” with many possible reasons for Error 111: "connection refused" showing “some time”. As part of the python code that executes when certain commands are received, it actually changes the network configuration (for example, it calls commands such as ip , tc and iptables with various arguments) using a network connection to localhost - this is something that probably follows Avoid, causing serious debugging and generally unpleasant problems. In addition to the part where it is not needed, since the second program runs on the same computer, therefore, for any inter-program communication, you do not need to use network interfaces.