POULibrary.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 28 Apr 2016 13:05:57 +0300
changeset 1507 d7f474d10210
parent 772 98786137232d
child 1511 91538d0c242c
permissions -rw-r--r--
fix issue with sometimes wrong return code of ProcessLogger


As a result of wrong return code Beremiz gives folowing traceback:
Traceback (most recent call last):
File "./Beremiz.py", line 850, in OnMenu
getattr(self.CTR, method)()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 925, in _Build
IECGenRes = self._Generate_SoftPLC()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 568, in _Generate_SoftPLC
return self._Compile_ST_to_SoftPLC()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 661, in _Compile_ST_to_SoftPLC
C_files.remove("POUS.c")
ValueError: list.remove(x): x not in list

The problem is that both threads (for reading stdout and stderr) call self.Proc.poll(),
that updates internal returncode field. This call is done without any locking and the first thread gets correct result,
but other gets 0 as retval. If 0 gets thread, that afterwards calls callback finish, then wrong return code is returned
to the parent. Now only the thread with a callback polls for the return code, other thread just checked local value.

Additionally function spin() waits now until all threads finish reading their pipes, so the results are always correct.
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
     1
from weakref import ref
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
     2
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
     3
class POULibrary:
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
     4
    def __init__(self, CTR, LibName, TypeStack):
772
98786137232d Fixed import dependency order for POUlibrary and PLCControler
Edouard Tisserant
parents: 732
diff changeset
     5
        from PLCControler import PLCControler
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
     6
        self.CTR = ref(CTR)
731
4fc681ed0c61 refecored library extension machanism
Edouard Tisserant
parents: 728
diff changeset
     7
        self.LibName = LibName
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
     8
        self.LibraryControler = PLCControler()
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
     9
        self.LibraryControler.OpenXMLFile(self.GetLibraryPath())
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    10
        self.LibraryControler.ClearConfNodeTypes()
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    11
        self.LibraryControler.AddConfNodeTypesList(TypeStack)
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    12
        self.program = None;
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    13
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    14
    def GetSTCode(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    15
        if not self.program:
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    16
            self.program = self.LibraryControler.GenerateProgram()[0]+"\n"
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    17
        return self.program 
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    18
731
4fc681ed0c61 refecored library extension machanism
Edouard Tisserant
parents: 728
diff changeset
    19
    def GetName(self):
4fc681ed0c61 refecored library extension machanism
Edouard Tisserant
parents: 728
diff changeset
    20
        return self.LibName
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    21
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    22
    def GetCTR(self):
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    23
        return self.CTR()
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    24
        
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    25
    def GetTypes(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    26
        return {"name" : self.GetName(), "types": self.LibraryControler.Project}
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    27
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    28
    def GetLibraryPath(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    29
        raise Exception("Not implemented")
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    30
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    31
    def Generate_C(self, buildpath, varlist, IECCFLAGS):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    32
        # Pure python or IEC libs doesn't produce C code
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    33
        return ((""), [], False), ""