473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

cgi concurrency approaches?

I'm wondering if folks here have favorite lightweight ways of dealing
with concurrency in cgi's. Take a simple case:

You want to write a cgi that implements a simple counter. The first
time it's called, it prints "1". The next time, "2", etc. The naive
implementation looks something like:

fd = open("counter.num", "rw")
n = int(fd.read())
fd.seek(0, 0)
fd.write("%d"% n+1)
print "Content-type: text/html\n\n"
print n

but of course there's the obvious race condition if two people hit the
cgi at the same time.

Fancier solutions include running an transactional database in another
process and connecting to it, setting up a daemon that remembers the
counter value in memory and serializes access through a socket that
the cgi opens, using a file-system specific hack like linking to
counter file to lock it, having a timeout/retry if the counter is
locked, with a possible hangup if a lock file gets left around by
accident, etc. Each is a big pain in the neck.

Anyone have some simpler approaches?
Jul 18 '05 #1
5 2099
Paul Rubin wrote:
I'm wondering if folks here have favorite lightweight ways of dealing
with concurrency in cgi's. Take a simple case:

You want to write a cgi that implements a simple counter. The first
time it's called, it prints "1". The next time, "2", etc. The naive
implementation looks something like:

fd = open("counter.num", "rw")
n = int(fd.read())
fd.seek(0, 0)
fd.write("%d"% n+1)
print "Content-type: text/html\n\n"
print n

but of course there's the obvious race condition if two people hit the
cgi at the same time.

Fancier solutions include running an transactional database in another
process and connecting to it, setting up a daemon that remembers the
counter value in memory and serializes access through a socket that
the cgi opens, using a file-system specific hack like linking to
counter file to lock it, having a timeout/retry if the counter is
locked, with a possible hangup if a lock file gets left around by
accident, etc. Each is a big pain in the neck.

Anyone have some simpler approaches?


I'd just run Xitami (www.xitami.org) and connect a LRWP (Long-Running
Web Process). I'm not familliar with Apache, so I don't know if
mod_python is equivalent.

- Josiah

Jul 18 '05 #2
Paul Rubin <http://ph****@NOSPAM.invalid>:
I'm wondering if folks here have favorite lightweight ways of dealing
with concurrency in cgi's. Take a simple case:

You want to write a cgi that implements a simple counter. The first
time it's called, it prints "1". The next time, "2", etc. [...]Anyone have some simpler approaches?


Use mod_python and keep the counter in a Python variable (if the value
needs not be persistent).
http://www.modpython.org/

--
René Pijlman
Jul 18 '05 #3
On Fri, 2004-01-23 at 10:04, Rene Pijlman wrote:
Paul Rubin <http://ph****@NOSPAM.invalid>:
I'm wondering if folks here have favorite lightweight ways of dealing
with concurrency in cgi's. Take a simple case:

You want to write a cgi that implements a simple counter. The first
time it's called, it prints "1". The next time, "2", etc.

[...]
Anyone have some simpler approaches?


Use mod_python and keep the counter in a Python variable (if the value
needs not be persistent).
http://www.modpython.org/


That will only work if you use Apache 2 in threaded mode. Apache 1 and
Apache 2 in forking mode (multiple processes) will have a copy of the
counter object per-process.

Cheers, Matt

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenation.net

Any views expressed are my own and do not necessarily reflect the
views of my employer.
Jul 18 '05 #4
Matt Goodall:
Rene Pijlman:
Use mod_python and keep the counter in a Python variable


That will only work if you use Apache 2 in threaded mode.


Of course, good point.

--
René Pijlman
Jul 18 '05 #5
[replying to someone else's reply because I missed the original]

[Paul Rubin]
I'm wondering if folks here have favorite lightweight ways of dealing
with concurrency in cgi's, [snip] but of course there's the obvious race
condition if two people hit the
cgi at the same time.

Fancier solutions include running an transactional database in another
process and connecting to it, setting up a daemon that remembers the
counter value in memory and serializes access through a socket that
the cgi opens, using a file-system specific hack like linking to
counter file to lock it, having a timeout/retry if the counter is
locked, with a possible hangup if a lock file gets left around by
accident, etc. Each is a big pain in the neck.
Anyone have some simpler approaches?


I don't know about a platform independent solution, but if you were
willing to limit yourself to certain varieties of Unix, e.g. Linux, it
would probably be relatively easy to implement a persistent counter
using something like System V message queues, or shared memory +
semaphores. This obviously will work only on systems which support
System V IPC.

Here is a brief page about System V IPC

http://www.science.unitn.it/~fiorell...lk/node56.html

And here's a module, which I've never used, which purports to provide
a python interface to System V IPC facilities, if available.

http://www.theory.org/~mac4/software/

Given the above facilities, there are two ways I would approach the
problem of a persistent counter.

1. Use a dedicated Queue, and hold the counter value inside a single
message which "lives" on that queue. Those incrementing the counter
read the single message from the queue, increment it and put it back
on the queue. Any processes awaiting access to the "counter message"
would then do a blocking read on the queue. Since all puts and gets of
messages are atomic, you are guaranteed only atomic updates to your
counter. However, you could lock your system up if one of your
accessing CGI processes did not put the counter back again! You can
use timeouts as well, if my memory serves (it's been a long time since
I used System V IPC). You can also get fancy, with priorities, etc.

2. Store the value in pre-created shared memory partition, protected
by a semaphore. Since there are shared memory python modules for
several platforms, you might have a better chance with this approach
on non-unix platforms.

The Python Object Sharing (POSH) module might provide wrappers to some
useful functionality, although the module itself might be a little
heavyweight for providing a simple persistent counter.

http://poshmodule.sourceforge.net/posh/html/posh.html

HTH,

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Rivers | last post by:
Hello, The common approaches to concurrency control in web apps: optimistic: - row version (timestamp, guid, datetime, digest) etc. - value checking pessimistic: - locking fields /...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
4
by: Bob | last post by:
While testing my my program I came up with a consistency exception. My program consists of three datagridviews, One called dgvPostes which is the parent grid and its two children,one called...
7
by: William E Voorhees | last post by:
I'm updating an Access database in a windows multi-user environment. I'm using disconnected data I read data from an Access Data table to a data object I update the data object from a...
4
by: Andrew Robinson | last post by:
I am working on a system system that requires optimistic concurrency within a web app. At first I thought this would be easy. We generate our own entities and dal/service layer but I now see that...
3
by: John | last post by:
Hi I have a vs 2003 winform data app. All the data access code has been generated using the data adapter wizard and then pasted into the app. The problem I have is that I am getting a data...
10
by: e_matthes | last post by:
Hello everyone, I have read many threads about concurrency issues, and I think I understand some of the pieces, but not the whole picture. I believe I am like many people using php: ...
0
by: RKT | last post by:
I have a DataGridView bound to an MS Access table. This is a single- user application. When the User is adding or editing a row, the User may click on a Control elsewhere. That Control has context...
5
by: John | last post by:
Hi I have developed the following logic to handle db concurrency violations. I just wonder if someone can tell me if it is correct or if I need a different approach.Would love to know how pros...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.