multiple users doing form submission with python CGI
Question: I have a simple cgi script in python collecting a value from form fields
submitted through post. After collecting this, iam dumping these values to a
single text file.

Now, when multiple users submit at the same time, how do we go about it?

In C\C++ we use semaphore\mutex\rwlocks etc? Do we have anything similar in
python. Also, opening and closing the file multiple times doesnt seem to be a
good idea for every user request.

We have our code base for our product in C\C++. I was asked to write a simple
cgi script for some reporting purpose and was googling with python and cgi.

Please let me know.

Thanks! Santhosh


Answer: There are lots of python based servers you could use. Here's one:

[Twisted](http://twistedmatrix.com/trac/)

> Twisted is an event-driven networking engine written in Python and licensed
> under the open source
    
    
    from twisted.internet import protocol, reactor
    
    class Echo(protocol.Protocol):
        def dataReceived(self, data):
            self.transport.write(data)
    
    class EchoFactory(protocol.Factory):
        def buildProtocol(self, addr):
            return Echo()
    
    reactor.listenTCP(1234, EchoFactory())
    reactor.run()
    

You probably want to drop the values into a database instead of a text file.

However threading is available and so you can use `lock()` to ensure only one
user writes to the file at a time.

<http://effbot.org/zone/thread-synchronization.htm>

> Locks are typically used to synchronize access to a shared resource. For
> each shared resource, create a Lock object. When you need to access the
> resource, call acquire to hold the lock (this will wait for the lock to be
> released, if necessary), and call release to release it

