473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

multiple threads with Logging: ValueError: I/O operation on closedfile

OS: Solaris 9
Python Version: 2.4.4

I need to log certain data in a worker thread; however, I am getting
an error now when I use two worker threads.
I think the problem comes from the line
logging.info('Thread Object (%d):(%d), Time:%s in seconds %d'%
(self.no,self.duration,time.ctime(),time.time()))
when multiple worker thread is trying to update the log files.
What did I do wrong? Should I lock the log file before writing to
it? Thanks.

Traceback (most recent call last):
File "/tools/python-2_4_4.i386.solaris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file
Traceback (most recent call last):
File "/tools/python-2_4_4.i386.solaris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file
class Worker(threading.Thread):
def __init__(self,no,duration):
threading.Thread.__init__(self)
self.no = no
self.duration = duration

def run(self):
end = time.time() + self.duration

while(end time.time()):
logging.info('Thread Object (%d):(%d), Time:%s in seconds
%d'%(self.no,self.duration,time.ctime(),time.time( )))
time.sleep(10)
def main():
children = []
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %
(message)s',
filename='logs/myapp.log',
filemode='w')
args = parseArgs()

for i in range(args.threads):
logging.info('i=%d'%(i))
children.append(Worker(i,args.duration))
children[i].start()
time.sleep(0.1)

Nov 8 '08 #1
2 4059
On Nov 8, 10:52 pm, scriptlear...@gmail.com wrote:
OS: Solaris 9
Python Version: 2.4.4

I need to log certain data in a worker thread; however, I am getting
an error now when I use two worker threads.
I think the problem comes from the linelogging.info('Thread Object (%d):(%d), Time:%s in seconds %d'%
(self.no,self.duration,time.ctime(),time.time()))
when multiple worker thread is trying to update the log files.
What did I do wrong? Should I lock the log file before writing to
it? Thanks.

Traceback (most recent call last):
File "/tools/python-2_4_4.i386.solaris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file
Traceback (most recent call last):
File "/tools/python-2_4_4.i386.solaris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file

class Worker(threading.Thread):
def __init__(self,no,duration):
threading.Thread.__init__(self)
self.no = no
self.duration = duration

def run(self):
end = time.time() + self.duration

while(end time.time()):
logging.info('Thread Object (%d):(%d), Time:%s in seconds
%d'%(self.no,self.duration,time.ctime(),time.time( )))
time.sleep(10)

def main():
children = []
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %
(message)s',
filename='logs/myapp.log',
filemode='w')
args = parseArgs()

for i in range(args.threads):
logging.info('i=%d'%(i))
children.append(Worker(i,args.duration))
children[i].start()
time.sleep(0.1)
It would be helpful if you could post a complete script which shows
the problem. You should be doing a join for each of the threads
spawned in the main thread, as otherwise the main thread will exit
after the for loop and cause atexit processing to be done (which
causes logging handlers to be closed).

Regards,

Vinay Sajip
Nov 10 '08 #2
On Nov 8, 10:52 pm, scriptlear...@gmail.com wrote:
OS: Solaris 9
Python Version: 2.4.4

I need to log certain data in a worker thread; however, I am getting
an error now when I use two worker threads.
I think the problem comes from the linelogging.info('Thread Object (%d):(%d), Time:%s in seconds %d'%
(self.no,self.duration,time.ctime(),time.time()))
when multiple worker thread is trying to update the log files.
What did I do wrong? Should I lock the log file before writing to
it? Thanks.

Traceback (most recent call last):
File "/tools/python-2_4_4.i386.solaris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file
Traceback (most recent call last):
File "/tools/python-2_4_4.i386.solaris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file

class Worker(threading.Thread):
def __init__(self,no,duration):
threading.Thread.__init__(self)
self.no = no
self.duration = duration

def run(self):
end = time.time() + self.duration

while(end time.time()):
logging.info('Thread Object (%d):(%d), Time:%s in seconds
%d'%(self.no,self.duration,time.ctime(),time.time( )))
time.sleep(10)

def main():
children = []
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %
(message)s',
filename='logs/myapp.log',
filemode='w')
args = parseArgs()

for i in range(args.threads):
logging.info('i=%d'%(i))
children.append(Worker(i,args.duration))
children[i].start()
time.sleep(0.1)
Take a look at this example test script to see how to use logging in a
multi-threaded environment:

http://dpaste.com/hold/89734/

Regards,

Vinay Sajip
Nov 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: max | last post by:
I have an application which calls a module runx, in runx I import logging and get a logger add handlers. I want to be able to rename the file created by fileHandler. several problems, I can't...
5
by: j vickroy | last post by:
Hello, I'm trying to understand the behavior of the Python 2.3 logging module (MS Windows 2k) with regard to RotatingFileHandler. The following script illustrates a puzzling problem. What is...
0
by: Neil Benn | last post by:
Hello, I'm running a test and having issues with logging, if I call logging.shutdown() and then want to start the logging going again then I get a problem as if I call shutdown, I can't get the...
2
by: flupke | last post by:
Hi, i have a class and a class attribute log which is a logger object. In the __del__() function i want to log a message but it fails even if i use self.__class__.log. The error i get is...
1
by: Oliver Eichler | last post by:
Hi, I experience several exceptions from python's logging system when using the rollover feature on Windows. Traceback (most recent call last): File "c:\Python24\lib\logging\handlers.py",...
1
by: Chris Curvey | last post by:
Hi all, I've apparently tied myself up a bit using the logging package. In my project, I have a core set of model and controller classes that set up their logging using logging.fileConfig()....
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
1
by: s99999999s2003 | last post by:
hi i have defined a function def logger(logfile,msg): import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y...
3
by: scriptlearner | last post by:
I am trying to put up a queue (through a logging thread) so that all worker threads can ask it to log messages. However, the problem I am facing is that, well, the logging thread itself is running...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.