How to prevent from race conditions to share data between many process and thread in python

M

mars

I use TurboGears to do some web service. TurboGears use cherrypy. When
web browser access this site, the cherrypy will call my python
program. So my program looks like a lib. When web browser access the
site, the http server will fock a process or gerenate a thread. I need
share some data or operate some files. How can I prevent from race
conditions. Is there any way can I lock this.
Thank you in advance!
 
D

Diez B. Roggisch

mars said:
I use TurboGears to do some web service. TurboGears use cherrypy. When
web browser access this site, the cherrypy will call my python
program. So my program looks like a lib. When web browser access the
site, the http server will fock a process or gerenate a thread. I need
share some data or operate some files. How can I prevent from race
conditions. Is there any way can I lock this.
Thank you in advance!

There are the Lock and RLock objects available in the module threading.

Diez
 
D

Dennis Lee Bieber

I use TurboGears to do some web service. TurboGears use cherrypy. When
web browser access this site, the cherrypy will call my python
program. So my program looks like a lib. When web browser access the
site, the http server will fock a process or gerenate a thread. I need
share some data or operate some files. How can I prevent from race
conditions. Is there any way can I lock this.
Thank you in advance!

Since you don't expand upon the type of data you need to "share", it
is a touch difficult to respond. However, assuming you can not isolate
the data/files by some session key/cookie ID, the next route is probably
to place that data into something designed for concurrent access --
meaning: use some RDBM system and let IT handle concurrent access (of
course, you will have to code your procedures to handle RDBM status
codes indicating updates by another client). Or, though it is likely the
most complicated to implement, you have a long-running/persistent
process acting as a server -- all accesses to shared data would be
handled by sending requests/commands to this server process; since it
only handles one request at a time, you can avoid "unseen" updates to
the data [you may still have the problem of losing an update if two
instances both send "set x to y" commands -- this is what the RDBM would
catch and report].
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Laurent Pointal

mars a écrit :
I use TurboGears to do some web service. TurboGears use cherrypy. When
web browser access this site, the cherrypy will call my python
program. So my program looks like a lib. When web browser access the
site, the http server will fock a process or gerenate a thread. I need
share some data or operate some files. How can I prevent from race
conditions. Is there any way can I lock this.
Thank you in advance!

See in the cookbook:

http://aspn.activestate.com/ASPN/search?query=locking&x=0&y=0&section=PYTHONCKBK&type=Subsection

And test/choose one solution...
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252495
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
....

A+

Laurent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top