Multiple Thread Synchronization

Bala blk at srasys.co.in
Sat Jul 24 11:22:08 CEST 2004


Hi,
    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 SampleApplication.py file and click the  "Run Multiple Thread"
you will see the above exception "ValueError: I/O operation on closed file"

First One is SampleApplication.py(this 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):
  self.CreateThread()
  
 def OnExit(self, event):
  self.Close()

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


 def OnThreadEnded(self, evt):
  try:
   if self.mainLock is None:
    self.mainLock = thread.allocate_lock()
   self.mainLock.acquire() 
   print 1
   self.mainLock.release()  
  except:
   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
  try:
##   self.mLock.acquire()
   self.identifier = thread.start_new_thread(self.Run, ())
##   self.mLock.release()
  except:
   return 0

 def Run(self):
  try:
   while (self.threadRunning):
    print "In Separate Thread  ", self.threadCounter
##    self.mLock.acquire()
    print "Lock =", self.mLock
    self.testSysStdOut.Run()
    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) 
  editText.CenterOnScreen()
  editText.ShowModal()
  editText.Destroy()
  return True

  
app = myApp(0)
app.MainLoop() 

## End of First File
#-------------------------------------------------------------------------------------------------------------------------------


Second File Name is SysStdOut.py(this 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):
        self.lock.acquire()
        oldStdOut = sys.stdout
        fileName = "SysStdOut" + str(thread.get_ident())
        tempStd = sys.stdout = open("c:\\" + fileName + ".txt", "w")
        self.ConsolePrint()
        sys.stdout = oldStdOut
        tempStd.close()
        self.lock.release()

        
    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: <http://mail.python.org/pipermail/python-list/attachments/20040724/af211b3a/attachment.html>


More information about the Python-list mailing list