Problems writing to file

Bernard Lebel 3dbernard at gmail.com
Tue Mar 21 22:49:50 CET 2006


Hi Gary,

That is actually what I do. Each time the program prints something to
the file, it flushes the buffer. I also tried opening and closing the
file, same result.

I could indeed post the code. That is only a fraction of the program,
as the log is managed through the Queue module:




def __nodeLog( self, iCallerLevel, sJobID, sMsg ):
		
		"""
		The method checks for an override in loglevel given by command line argument.
		If such override has been set (using the 'loglevel' argument), then
the command line value is used.
		
		ARGUMENTS:
		iCallerLevel (integer): level of verbosity to use, applies only to
node type of logging
		sJobID (string): the JobID of the job, if applies
		sMsg (string): node line to print
		"""
		
		# Check if a log level was set through command line
		if 'loglevel' in dCmdLineArgs:
			# Log level set through command line, override any other log level
			sLogLevel = dCmdLineArgs[ 'loglevel' ]
			iLogLevel = int( sLogLevel )
		else:
			iLogLevel = self.loglevel
		
		
		
		if iCallerLevel <= iLogLevel:
			
			# Forge line to print into node log
			sLine = '%s %s [jobid: %s]: %s\n' % ( NAME, time.ctime(), str(
sJobID ), sMsg )
			if self.printmsg == True: print sLine
			
			
			# Check if user specified extraction command line arguments
			for sArg, tValue in dCmdLineArgs.items():
				if 'extractlog' in sArg:
					sLogLevel = tValue[0]
					sLogType = tValue[1]
					
					# Check if caller level is same as log level defined by the
extract argument
					if str( iCallerLevel ) == sLogLevel:
						sExtractLogFile = os.path.join( self.NODELOGS, '%s_%s.log' % (
NAME, sLogType ) )
						self.__output( sExtractLogFile, sLine )
			
			
			self.__output( self.MAINLOGFILENAME, sLine )





def __output( self, sLogFile, sLine ):
		
		"""
		Try to write to the log file.
		
		ARGUMENTS:
		sLogFile (string): the log file
		sPrint (string): the line to print
		"""
		
		# Try 3 times to write to the output file
		
		for i in range(3):
			
			# Try to output the message
			
			try:
				oLogFile = file( sLogFile, 'a+' )
				oLogFile.write( sLine )
				oLogFile.close()
				
				return LOGOPSUCCESS
			
			except IOError:
				if i == 2:
					if self.printmsg == True:
						print '\nERROR > log.__output : All attempts to write to log
file %s failed, latest updates will be lost.\n' % ( sLogFile )
					return LOGOPFAILURE
				else:
					if self.printmsg == True:
						print '\nWARNING > log.__output : Attempt "%i" to write to log
file %s raised IO Error, next attempt in %s seconds.\n' % ( i+1,
sLogFile, self.LOGWAITTIME )




Thanks
Bernard



On 3/21/06, Gary Herron <gherron at islandtraining.com> wrote:
> Bernard Lebel wrote:
>
> >Hello,
> >
> >I have this strange problem. I have written a program that writes a
> >log to a log file.
> >
> >To start the program (I'm on Linux Fedora Core 3), I used a service.
> >This service runs a bash file, wich in turn runs the Python top
> >program file.
> >
> >Sooner or later the program stops writing to the log file. I have
> >absolutely no clue why would that happen. Since the output of the
> >Python program is not redirected other than to this log file, I have
> >no idea why it's doing this. But the Python process appears still
> >alive and well!
> >
> >But the strange thing is that if I run the program directly from a
> >bash shell, no problem whatsoever, the programs runs smoothly and
> >never stops writing to the file, until I kill it.
> >
> >
> >Any suggestion?
> >
> >
> >
> >Thanks in advance
> >Bernard
> >
> >
> Perhaps ...
>
> Output is normally buffered in memory until some threshhold is reached,
> at which point the whole buffer is written, and the process repeats. If
> your output quantity is small, and your impatience is high, you may be
> declaring failure before the buffer fills and triggers a write. The
> solution would be to call flush on the output file after each write (or
> perhaps just wait more patiently).
>
> If the output is voluminous, then it's probably something else ... but I
> don't know what. Perhpas you could post your code.
>
> Gary Herron
>
>



More information about the Python-list mailing list