示例#1
0
    def setTransmissionData(self, data):
        if data is None:
            return False
        data, isFirst, isLast = data

        # In-memory case
        if self.__fileName is None:
            if isFirst:
                self.__incomingTransmissionFile = bytes()
            elif self.__incomingTransmissionFile is None:
                return False
            self.__incomingTransmissionFile += data
            if isLast:
                self.__inMemorySerializedData = self.__incomingTransmissionFile
                self.__incomingTransmissionFile = None
                return True
            return False

        # File case
        tmpFile = self.__fileName + '.1.tmp'
        if isFirst:
            if self.__incomingTransmissionFile is not None:
                self.__incomingTransmissionFile.close()
            try:
                self.__incomingTransmissionFile = open(tmpFile, 'wb')
            except:
                LOG_WARNING('Failed to open file for incoming transition')
                LOG_CURRENT_EXCEPTION()
                self.__incomingTransmissionFile = None
                return False
        elif self.__incomingTransmissionFile is None:
            return False
        try:
            self.__incomingTransmissionFile.write(data)
        except:
            LOG_WARNING('Failed to write incoming transition data')
            LOG_CURRENT_EXCEPTION()
            self.__incomingTransmissionFile = None
            return False
        if isLast:
            self.__incomingTransmissionFile.close()
            self.__incomingTransmissionFile = None
            try:
                atomicReplace(tmpFile, self.__fileName)
            except:
                LOG_WARNING('Failed to rename temporary incoming transition file')
                LOG_CURRENT_EXCEPTION()
                return False
            return True
        return False
示例#2
0
    def setTransmissionData(self, data):
        if data is None:
            return False
        data, isFirst, isLast = data

        # In-memory case
        if self.__fileName is None:
            if isFirst:
                self.__incomingTransmissionFile = bytes()
            elif self.__incomingTransmissionFile is None:
                return False
            self.__incomingTransmissionFile += data
            if isLast:
                self.__inMemorySerializedData = self.__incomingTransmissionFile
                self.__incomingTransmissionFile = None
                return True
            return False

        # File case
        tmpFile = self.__fileName + '.1.tmp'
        if isFirst:
            if self.__incomingTransmissionFile is not None:
                self.__incomingTransmissionFile.close()
            try:
                self.__incomingTransmissionFile = open(tmpFile, 'wb')
            except:
                logging.exception('Failed to open file for incoming transition')
                self.__incomingTransmissionFile = None
                return False
        elif self.__incomingTransmissionFile is None:
            return False
        try:
            self.__incomingTransmissionFile.write(data)
        except:
            logging.exception('Failed to write incoming transition data')
            self.__incomingTransmissionFile = None
            return False
        if isLast:
            self.__incomingTransmissionFile.close()
            self.__incomingTransmissionFile = None
            try:
                atomicReplace(tmpFile, self.__fileName)
            except:
                logging.exception('Failed to rename temporary incoming transition file')
                return False
            return True
        return False
示例#3
0
    def serialize(self, data, id):
        if self.__pid != 0:
            return

        self.__currentID = id

        # In-memory case
        if self.__fileName is None:
            self.__inMemorySerializedData = zlib.compress(
                cPickle.dumps(data, -1))
            self.__pid = -1
            return

        # File case
        if self.__useFork:
            pid = os.fork()
            if pid != 0:
                self.__pid = pid
                return

        try:
            tmpFile = self.__fileName + '.tmp'
            if self.__serializer is not None:
                self.__serializer(tmpFile, data[1:])
            else:
                with open(tmpFile, 'wb') as f:
                    with gzip.GzipFile(fileobj=f) as g:
                        cPickle.dump(data, g, -1)

            atomicReplace(tmpFile, self.__fileName)
            if self.__useFork:
                os._exit(0)
            else:
                self.__pid = -1
        except Exception as e:
            if self.__useFork:
                os._exit(-1)
            else:
                self.__pid = -2
示例#4
0
    def serialize(self, data, id):
        if self.__pid != 0:
            return

        self.__currentID = id

        # In-memory case
        if self.__fileName is None:
            self.__inMemorySerializedData = zlib.compress(cPickle.dumps(data, -1))
            self.__pid = -1
            return

        # File case
        if self.__useFork:
            pid = os.fork()
            if pid != 0:
                self.__pid = pid
                return

        try:
            tmpFile = self.__fileName + '.tmp'
            if self.__serializer is not None:
                self.__serializer(tmpFile, data[1:])
            else:
                with open(tmpFile, 'wb') as f:
                    with gzip.GzipFile(fileobj=f) as g:
                        cPickle.dump(data, g, -1)

            atomicReplace(tmpFile, self.__fileName)
            if self.__useFork:
                os._exit(0)
            else:
                self.__pid = -1
        except Exception as e:
            if self.__useFork:
                os._exit(-1)
            else:
                self.__pid = -2