473,791 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('T hread Object (%d):(%d), Time:%s in seconds %d'%
(self.no,self.d uration,time.ct ime(),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.sola ris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.wri te(fs % msg)
ValueError: I/O operation on closed file
Traceback (most recent call last):
File "/tools/python-2_4_4.i386.sola ris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.wri te(fs % msg)
ValueError: I/O operation on closed file
class Worker(threadin g.Thread):
def __init__(self,n o,duration):
threading.Threa d.__init__(self )
self.no = no
self.duration = duration

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

while(end time.time()):
logging.info('T hread Object (%d):(%d), Time:%s in seconds
%d'%(self.no,se lf.duration,tim e.ctime(),time. time()))
time.sleep(10)
def main():
children = []
logging.basicCo nfig(level=logg ing.INFO,
format='%(ascti me)s %(levelname)s %
(message)s',
filename='logs/myapp.log',
filemode='w')
args = parseArgs()

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

Nov 8 '08 #1
2 4087
On Nov 8, 10:52 pm, scriptlear...@g mail.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.inf o('Thread Object (%d):(%d), Time:%s in seconds %d'%
(self.no,self.d uration,time.ct ime(),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.sola ris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.wri te(fs % msg)
ValueError: I/O operation on closed file
Traceback (most recent call last):
File "/tools/python-2_4_4.i386.sola ris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.wri te(fs % msg)
ValueError: I/O operation on closed file

class Worker(threadin g.Thread):
def __init__(self,n o,duration):
threading.Threa d.__init__(self )
self.no = no
self.duration = duration

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

while(end time.time()):
logging.info('T hread Object (%d):(%d), Time:%s in seconds
%d'%(self.no,se lf.duration,tim e.ctime(),time. time()))
time.sleep(10)

def main():
children = []
logging.basicCo nfig(level=logg ing.INFO,
format='%(ascti me)s %(levelname)s %
(message)s',
filename='logs/myapp.log',
filemode='w')
args = parseArgs()

for i in range(args.thre ads):
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...@g mail.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.inf o('Thread Object (%d):(%d), Time:%s in seconds %d'%
(self.no,self.d uration,time.ct ime(),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.sola ris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.wri te(fs % msg)
ValueError: I/O operation on closed file
Traceback (most recent call last):
File "/tools/python-2_4_4.i386.sola ris.5_9/lib/python2.4/logging/
__init__.py", line 737, in emit
self.stream.wri te(fs % msg)
ValueError: I/O operation on closed file

class Worker(threadin g.Thread):
def __init__(self,n o,duration):
threading.Threa d.__init__(self )
self.no = no
self.duration = duration

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

while(end time.time()):
logging.info('T hread Object (%d):(%d), Time:%s in seconds
%d'%(self.no,se lf.duration,tim e.ctime(),time. time()))
time.sleep(10)

def main():
children = []
logging.basicCo nfig(level=logg ing.INFO,
format='%(ascti me)s %(levelname)s %
(message)s',
filename='logs/myapp.log',
filemode='w')
args = parseArgs()

for i in range(args.thre ads):
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
1421
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 seem to be able to rename the file, calling shutdown, or removeHandler and then hndlr.close(), I get No handlers could be found for logger "root" even though I am not calling any related methods. The second problem I find if I re-enter my module...
5
11278
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 wrong with this script? Thanks, -- jv
0
1679
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 root logger again, such as : ..>>> import logging ..>>> objTestLogger = logging.getLogger() ..>>> objTestLogger.setLevel(logging.INFO)
2
3329
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 this: Traceback (most recent call last): File "C:\Python24\lib\logging\__init__.py", line 712, in emit self.stream.write(fs % msg)
1
2371
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", line 62, in emit if self.shouldRollover(record): File "c:\Python24\lib\logging\handlers.py", line 132, in shouldRollover self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
1
1697
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(). So far, so good. But I use these core classes from a bunch of different places. Sometimes from within a CherryPy server, sometimes batch jobs run from a command line, sometimes from Windows services (also written in
7
8590
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 i get these errors: Traceback (most recent call last): File "C:\Python24\lib\logging\handlers.py", line 71, in emit if self.shouldRollover(record):
1
385
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 %H:%M:%S', filename='%s' % (logfile),
3
5742
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 forever. It does not know when it should exit. Any suggestion? None of the worker threads knows for sure when the logging thread should exit since they don't know what other worker threads are doing. I also try to set a time limit for the...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.