views:

51

answers:

0

Hello every body,

I work on a Web text-to-speech System, trasforming text documents in audio mp3 files, using python 2.5. I use Apache2.2 as a server and mod_python as module to embed the Python interpreter within the server.

The user should submit via web interface (input interface), a file txt or a Word document (.doc or .rtf) and the system produce downloadable mp3 files (representing vocal version of written parts of the document); mp3 files are downloadable from the resulting web interface (output interface), generated by the system.

My problem is solving concurrent access to web system by multi user submitting document, in the same time.

The problem is that COM operations (using windows application like Word) to write or manipulate documents are not atomic. Also files operations generating mp3 files, are not atomic. So probably it is necessary a mutex/semaphore to permit unique access to critical resorces...

System use win32com library for opening/closing/manipulating the submitted documents with Word (not visible) in python.

Input Web interface, basically use a form to submit a document

<form action="uploadform.py/email" method="POST" enctype="multipart/form-data">
...
File: <input type="file" name="uFile" size="100"><br/><br/>
<input type="image" src="buttonUpload.jpg" value="Upload"> </form>

and the python handler uploadform.py try to use a mutex/semaphore to manage concurrency using also CoinitializeEx/UnCoinitialize functions; handler has a structure like:

import sys
sys.coinit_flags = 0
import threading 
import  _apache
import random
…
threadLock = threading.Lock()
…
*Code with some Functions call by principal email(…)*
…
#main function called through the web form
def email(req, uFile):   
    global threadLock   

#Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.

    pythoncom.CoInitializeEx(sys.coinit_flags)
    #richiesta acquisisce semaforo
    threadLock.acquire()
    …

    #main branch
    try:
          …
          Code to open/elaborate word documents o txt files (calling other function)
          ...
          generate mp3 files
          …
    #branch to manage exception
    except:
          …
    #Finally always executed
    finally:
          …
          #It is necessary close files
          if(threadLock != None):
              #release mutex
              threadLock.release()

    #Closes the COM library on the current thread, frees any other resources that the thread maintains 
    pythoncom.CoUninitialize()

Unfortunately, there is something wrong in managing concurrent access and so the system crash when more users access to the system in the same time. (I was supposing the system support a queue automatically by use of Coinitialize)... on Internet there is not so much documentation. :-(

Someone has experience on this kind of problem managing multi access to critical resources and can help me?

I don't know where to break my head... :-(

Some body has told me the probably use of cgi (instead of mod_python handler) could solve the thing (even if more slow), because it manage one request as one thread separate.

I don't know if it is true...

however i'm trying also to convert the handler in a cgi, but apache give internal server error!

Thanks for your precious answers.

Giulio