Memory leak using pywin32com for opc

淺唱寂寞╮ 提交于 2019-12-11 03:56:48

问题


I am having a hard time trying to figure out how to address leaking memory. I think this might be an issue with pywin32 but I am not completely sure.

My code to do reading/writing individual items seems to work just fine, but when using group functions it slowly leaks memory. I suspect this is from the 1-based array that must be passed in the server_handles.

Does anyone know of a work around?

def read_group(self, group, mode=OPC_SYNC, source=OPC_DS_CACHE):
    """ Read a group returning a list of Result tuples or None. """
    if group not in self.groups:
        raise OPCError("Group does not exist, add first.")

    items = []
    server_handles = []
    for name, item in self.items.items():
        if item.group == group:
            items.append(name)
            server_handles.append(item.server_handle)

    if not server_handles:
        return None

    # Note that arrays are 1 based so must add an element to beginning
    server_handles.insert(0, 0)

    if mode == OPC_SYNC:
        val, err, qual, ts = self.groups[group].SyncRead(
                Source=source,
                NumItems=len(server_handles) - 1,
                ServerHandles=server_handles)

        # Map the return arrays into a list of result tuples
        result = []
        for i in range(len(items)):
            result.append(Result(
                    items[i],
                    val[i],
                    OPC_QUALITY[qual[i]],
                    str(ts[i]),
                    err[i],
                    ))

        return result

来源:https://stackoverflow.com/questions/24535483/memory-leak-using-pywin32com-for-opc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!