FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

Use Existing Session #1176

Open biswaKL opened 3 years ago

biswaKL commented 3 years ago

Every time i connect my python code, one session gets added up. Its not using the existinf connection. Need little help.

image

My Code:

import sys
import logging
import time
from opcua import Client
from opcua import ua

class SubHandler(object):

    def datachange_notification(self, node, val, data):
        print("Python: New data change event", node, val)

    def event_notification(self, event):
        print("Python: New event", event)

if __name__ == "__main__":
    logging.basicConfig(level=logging.WARN)
    client = Client("opc.tcp://192.168.8.192:48020/", timeout=5)
    client.keepalive = False
    while True:
        try:
            client.connect()
            # client.load_type_definitions()  # load definition of server specific structures/extension objects
            root = client.get_root_node()
            print("Root node is: ", root)
            objects = client.get_objects_node()
            print("Objects node is: ", objects)

            # Node objects have methods to read and write node attributes as well as browse or populate address space
            print("Children of root are: ", root.get_children())

            myvar = client.get_node("ns=4;s=Demo.BoilerDemo.Boiler1.TemperatureSensor.Temperature")
            print(myvar.get_value())
            handler = SubHandler()
            sub = client.create_subscription(500, handler)
            handle = sub.subscribe_data_change(myvar)
            is_connected = True
            while True:
                if not client.uaclient._uasocket._thread.isAlive():
                    raise Exception
        except:
            print("Disconnected")
            try:
                client.disconnect()
            except:
                pass
        time.sleep(1)
AndreasHeine commented 3 years ago

reuse of session is not implemented! the reason why you stack up sessions is the session timeout of the opcua server... maybe to long default was 1 hour i guess depends on the server