0

When i'm trying to publish some messages to a broker i got the following error:

Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/site-packages/opcua/client/client.py", line 66, in run
    self.client.open_secure_channel(renew=True)
  File "/usr/local/lib/python3.8/site-packages/opcua/client/client.py", line 325, in open_secure_channel
    result = self.uaclient.open_secure_channel(params)
  File "/usr/local/lib/python3.8/site-packages/opcua/client/ua_client.py", line 265, in open_secure_channel
    return self._uasocket.open_secure_channel(params)
  File "/usr/local/lib/python3.8/site-packages/opcua/client/ua_client.py", line 197, in open_secure_channel
    future = self._send_request(request, message_type=ua.MessageType.SecureOpen)
  File "/usr/local/lib/python3.8/site-packages/opcua/client/ua_client.py", line 72, in _send_request
    self._socket.write(msg)
  File "/usr/local/lib/python3.8/site-packages/opcua/common/utils.py", line 118, in write
    self.socket.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe

i'm using network loop after client connection, and i tried to handle the error but, it deosn't work i have always the same issue, my fonction is called many times per second, each time when i recieve a data change notification from the server. Can someone help please? Here the following code that causes the error:

def clientPub(self,top,msg):
    try: 
        cl=self.client
        if(self.connectionFlag !=1):
            cl.connect(self.broker,1883)
            cl.loop_start()            
        cl.publish(top,msg,2)
    except IOError as e: 
        if e.errno == errno.EPIPE:
            print("Trying again to publish the message ...")
            cl.publish(top,msg,2) 

the above function is called by this one for every notification from the server:

def datachange_notification(self, node, val, data):
    print("data recieved :",data.monitored_item.Value.SourceTimestamp,val,machine_number+".G3_"+node.nodeid.Identifier[3:])        
    valuesDict={"name":machine_number+".G3_"+node.nodeid.Identifier[3:],"datapoint":[[str(data.monitored_item.Value.SourceTimestamp),val,3]],"attributes":{"machine-type":"opcua"}}   
    finalvalue={"messageId":str(round(time.time()*1000)),"body":valuesDict}
    sentval=json.dumps(finalvalue)
    self.clientPub(self.topic,sentval)

waiting for you reply, Thank you.

Panagiotis Simakis
  • 1,245
  • 1
  • 18
  • 45
RiyadhG
  • 1
  • 2
  • The stacktrace is showing in the `ua_client` package not the MQTT library. – hardillb May 19 '20 at 07:34
  • yep @Panagiotis, i noticed that yesterday when i was trying to solve the problem, but still not findind the solution of the broken pipe. thank you – RiyadhG May 19 '20 at 08:34

1 Answers1

0

This is a classical Producer-Consumer problem.

You are producing and storing data here in sentval and at same time trying to consume sentval by calling : self.clientPub(self.topic,sentval)

I also had similar problem and I solved using Producer-Consumer Threading Using Lock

It worked perfectly fine for me!

  • Actually this error only occurs when you try to do multiprocessing on some code with errors in it. It's unexpected that you face with this issue when your code is right. – M.B. Explorer May 26 '20 at 23:21