Multiple Thread Synchronization

Bala blk at
Sat Jul 24 11:22:08 CEST 2004

    1)Iam launching an application
    2)In that iam having two buttons
    3)First Button Name is "Run Multiple Thread"
    4)Second Button is "Exit"

    If i click the "Run Multiple Thread" Button,
    1)Iam creating multiple thread
    2)Each thread try to redirect the sys.stdout to the file
While redirecting to the file, i got this exception.
"ValueError: I/O operation on closed file"

I will be happy, if anyone address this problem...i tried to solve this problem for past fivedays..i couldnot...
Even i put a lock, the above exception is coming.Iam attaching my code kindly go through the code....let me know the problem..

I have two python file.....copy both the below file in a same folder and double click the file and click the  "Run Multiple Thread"
you will see the above exception "ValueError: I/O operation on closed file"

First One is is the main application file)
import sys
import threading
import thread
from wxPython.lib import newevent
from wxPython.wx import *   
import os
import time
from SysStdOut import *

ID_LAUNCH = wxNewId()
ID_EXIT = wxNewId()

ThreadEndedEvent, EVT_THREAD_ENDED = newevent.NewEvent()

class EditTextAppn(wxDialog):
 def __init__(self,parent,ID,title,pos=wxDefaultPosition,size=wxDefaultSize,style=wxDEFAULT_DIALOG_STYLE):
  pre = wxPreDialog()
  pre.Create(parent, ID, title, pos, wxSize(350,150), style)
  self.this = pre.this
  self.mainLock = None

  wxStaticText(self, -1, "By Clicking the Run Multiple Thread button, multiple thread \n will create and each thread  will try to acess the sys.stdout \n and redirect to the file. ",wxPoint(15, 9))
  wxButton(self, ID_LAUNCH, " Run Multiple Thread ",wxPoint(40,60))
  EVT_BUTTON(self, ID_LAUNCH, self.OnLaunch)
  wxButton(self, ID_EXIT, " Exit ",wxPoint(190,60))
  EVT_BUTTON(self, ID_EXIT, self.OnExit)

 def OnLaunch(self,event):
 def OnExit(self, event):

 def CreateThread(self):
  self.threadObj = []
  for i in range(10):
   self.threadObj.append(_SeparateThread(self, i))
   EVT_THREAD_ENDED(self, self.OnThreadEnded)
   for eachThread in self.threadObj:
   exceptionInfo = sys.exc_info()
   print str(exceptionInfo[0]), " " , str(exceptionInfo[1])

 def OnThreadEnded(self, evt):
   if self.mainLock is None:
    self.mainLock = thread.allocate_lock()
   print 1
   exceptionInfo = sys.exc_info()
   print str(exceptionInfo[0]), " " , str(exceptionInfo[1])

class _SeparateThread:
 def __init__(self, mThread, counter):
  self.mThread = mThread
  self.testSysStdOut = SysStdOut()
  self.threadCounter = counter
  self.mLock = threading.Semaphore()
 def Start(self):
  self.threadRunning = True
##   self.mLock.acquire()
   self.identifier = thread.start_new_thread(self.Run, ())
##   self.mLock.release()
   return 0

 def Run(self):
   while (self.threadRunning):
    print "In Separate Thread  ", self.threadCounter
##    self.mLock.acquire()
    print "Lock =", self.mLock
    self.threadRunning = False
##    self.mLock.release()
    print "Lock Release = ", self.mLock
  except :
   exceptionInfo = sys.exc_info()
   print str(exceptionInfo[0]), " " , str(exceptionInfo[1])
   self.threadRunning = False
   return 0

class myApp(wxApp):
 def OnInit(self):
  editText = EditTextAppn(None, -1, "Sample EditText Application", size=wxSize(350, 200),style = wxDEFAULT_DIALOG_STYLE) 
  return True

app = myApp(0)

## End of First File

Second File Name is contains the sys.stdout redirection code)
import sys
import threading
import thread
import time

class SysStdOut:
    def __init__(self):
        self.lock = threading.Semaphore()

    def Run(self):
        oldStdOut = sys.stdout
        fileName = "SysStdOut" + str(thread.get_ident())
        tempStd = sys.stdout = open("c:\\" + fileName + ".txt", "w")
        sys.stdout = oldStdOut

    def ConsolePrint(self):
        randObj = Random()
        print "Sys.stdout is Redirected to the File  ",randObj.randint(1,10)
        print "Sys.stdout is Redirected to the File  ",randObj.randint(1,10)
        print "Sys.stdout is Redirected to the File  ",randObj.randint(1,10)
        print "Sys.stdout is Redirected to the File  ",randObj.randint(1,10)

## End of Second File


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list