Re: Webware/Apache Threading Problem?

Christoph Zwerschke <[email protected]>
Newsgroups gmane.comp.python.webware
Message-ID <[email protected]>
John Dickinson wrote:
> Maybe I'm a little dense here, but I'm not seeing why os.chdir is such a 
> bad thing in a threaded environment. I did a little google-ing, but I 
> did not find anything the was talking about problems in a threaded 
> environment.

The following program shows the problem quite clearly:

----------------------------

from os import mkdir, chdir
from time import sleep
from threading import Thread

try:
     # make test dirs
     mkdir('dir1')
     mkdir('dir1/dir2')
     open('dir1/test', 'w').write('inside dir1')
     open('dir1/dir2/test', 'w').write('inside dir2')
except:
     pass

def thread1():
     chdir('dir1')
     sleep(0.2)
     print "test file in dir1:", open('test').read()

def thread2():
     sleep(0.1)
     chdir('dir2')

Thread(target=thread1).start()
Thread(target=thread2).start()

----------------------------

 From looking at the thread1 function, you would expect "inside dir1" as 
output, but because thread2 is interfering, it prints "inside dir2".

I have added the sleep calls to make the behavior predictable. In a real 
threaded environment, the output would be unpredictable.

-- Chris

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.