I am trying to establish a communication pipe between a C executable and python script.
Below is the Python script, and the weird thing about this is the following. If I run
the python script as below, then the C executable NEVER sees any data on the socket.
However, if I uncomment the Reader class (ie., only writting to the socket is enabled)
then the C executeable ALWAYS receives the data on the socket. Has anyone an explanation why this is not working when the Reader and Writer thread are running simultaneously?
class Reader(Thread):
def __init__(self):
Thread.__init__(self)
self.daemon = True
def run(self):
while True:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/demo_socket")
nonce = s.recv(4)
s.close
if len(nonce) == 4:
print("Result received \n")
class Writer(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
while True:
payload = "Hello World"
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/demo_socket")
s.send(payload)
s.close()
reader = Reader()
writer = Writer()
reader.start()
writer.start()
try:
while True:
sleep(10000)
except KeyboardInterrupt:
print("Terminated")
EDIT:
As suggested I am trying to open the connection globally as below but that gives me two additional problems:
1) After sending data, only 4 bytes should be received on the Python end but it seems I am receiving an endless stream of data…
2) Where can I close this globally opened stream?
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/demo_socket")
class Reader(Thread):
def __init__(self):
Thread.__init__(self)
self.daemon = True
def run(self):
while True:
nonce = s.recv(4)
if len(nonce) == 4:
print("Result received \n")
class Writer(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
while True:
payload = "Hello World"
s.send(payload)
reader = Reader()
writer = Writer()
reader.start()
writer.start()
try:
while True:
sleep(10000)
except KeyboardInterrupt:
print("Terminated")
Your sender keeps on sending, and if the data you receive are replies to that, the server keeps on answering.
After you don’t need it any longer… Do you have a stop condition or something like that?