static variables - lock/semaphore

anil maran <anilmrn <at> yahoo.com>
2006-07-01 07:13:11 GMT

hi
i have a program that is run on a website, and as it
is run from a website, it can be called more than once
and hence could end up corrupting db/data
is there a way to use some sort of semaphore or lock
so that it is not accessed simultaneously
the problem is it can be done with static variables in
C but how is it done in python
thanks a lot
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Tutor maillist - Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Re: static variables - lock/semaphore

Alan Gauld <alan.gauld <at> freenet.co.uk>
2006-07-01 13:06:25 GMT

> i have a program that is run on a website, and as it
> is run from a website, it can be called more than once
> and hence could end up corrupting db/data
> is there a way to use some sort of semaphore or lock
> so that it is not accessed simultaneously
There are ways of doing this in Python but if you are using
a relational database for the data its usually easier to
apply row level locking at the database level.
> the problem is it can be done with static variables in
> C but how is it done in python
It could be done with a global variable in Python in the
same way but there are other solutions based around
threads which have explicit locks etc. For a web
application using Python threads would probably be
a good bet.
But you may find the whole problem simplified by adopting a
web framework such as TurboGears which will take care of
a lot of these kinds of issues for you - but with a bit of a
learning curve. But if you have several web apps to build
I'd seriously consider this route.
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
_______________________________________________

Re: static variables - lock/semaphore

Kent Johnson <kent37 <at> tds.net>
2006-07-01 14:30:56 GMT

anil maran wrote:
> hi
> i have a program that is run on a website, and as it
> is run from a website, it can be called more than once
> and hence could end up corrupting db/data
> is there a way to use some sort of semaphore or lock
> so that it is not accessed simultaneously
> the problem is it can be done with static variables in
> C but how is it done in python
If you are using a database for storage you should learn how to use the
transactional features of the database. They are designed to let you
control what happens when multiple users access the database at the same
time.
To protect access to other kinds of data look at threading.Lock and RLock.
Kent
_______________________________________________
Tutor maillist - Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Re: saving output in a text file (fwd)

Danny Yoo <dyoo <at> hkn.eecs.berkeley.edu>
2006-07-01 17:40:20 GMT

> If we want to create a text file and write some text into it we use the
> following command in Python:
>
> myfile = open("test.txt","w")<?xml:namespace prefix = o ns =
> "urn:schemas-microsoft-com:office:office" />
>
> myfile.write("hello world")
>
> But what if instead of writing the text 'hello world', i want to write
> the output returned by a regular expression pattern, into the text file,
> how would we specify that in the write command.
Just as a disclaimer: I am doing my utmost not to give out The Answer
here. So if it sounds like I'm not being very direct, that's precisely
because you have a good intuition. *grin* I'm trying to poke at the
source of the problem, and not the immediate side effects.
You still haven't tackled my initial question about:
> def double(s):
> """double: string -> string
> Doubles up the input string s. For example, double("abc")
> should return "abcabc".
> """
> return s + s
>
>
> Would you be able to write a program that takes "hello world", runs it
> through double(), and writes out "hello worldhello world" to disk?
Can you try this mini-problem first? What you've done doesn't touch on
the question: would you know how to use double() as a part of the write()?
Note that this is just a variation of your original question, replacing
the regex part with the use of a helper function. It seems like this
should be even easier than the problem you're having now.
Here's why I'm asking this: if you're getting stuck at this point too,
then I'll assume that the problem is that you are unfamiliar with using
helper functions and function composition. Then the group as a whole can
help you with that, since that's a fundamental programming skill that
you'll want to develop.
But if you can do the mini-exercise, then the focus shifts to the use of
regular expressions, and things become much easier, since it'll mean that
you're not familiar with the regex API.
_______________________________________________
Tutor maillist - Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor