Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old April 16th, 2004, 07:39 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 34 sec
Reputation Power: 0
file locking absolute on a website?

i've created a cgi prgrm with python and it allows users to input info on a regular basis to a .csv file and .txt. will the file lock up if two ppl try to access it simultaneously or if it is not finished closing? or does it matter that it is on a website ( a client/server architecture?) is there a way to circumvent this if only one person can access it a time - other than using a relational database.
ex: if one person is submit information can another be submiting at the same time and another be browsing the file - likethe administrator?

Thank you in advance
__________________
"In theory, there is no difference between theory and practice.
But, in practice, there is."


Reply With Quote
  #2  
Old April 20th, 2004, 08:10 AM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 34 sec
Reputation Power: 0
Anyone have this knowledge?

Reply With Quote
  #3  
Old April 20th, 2004, 09:22 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
You will have a problem.

Each new client accessing your website will get it's own thread/process whatever. This means you cannot guarantee the order in which data is written and occasionally attempts to open the file will fail as two or more client sessions write to the file.

Also the write is unlikely to be atomic and could be corrupted if the actual write is broken into smaller segments by the OS.


That is one reason for using a database - only one application actually writes the data to file. The write requests from the web site are queued by the database engine.

Just an idea
If your site is not busy then perhaps you could use a simple filelock . Where the filelock is a file just containing the process id or something eqaully small and unique with a simple checksum.

You want to write data:

Check if filelock exists. if it does, wait some time and check again.
Keep checking upto a check limit - if reached advise the user that site is busy or email the data to admin. Check the content of the file lock (and calc the checksum).

If the file lock does not exist, create it. Then read it back to make sure it is your filelock and not corrupted. If it isn't your lock wait some time again for the lock to disappear as above.

If it is your file lock then write data to your log file.
Then delete the filelock file.

I have no idea if this would work in practice or what the timing would be like.
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #4  
Old April 20th, 2004, 09:51 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,188 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 11 h 25 m 43 sec
Reputation Power: 251
The file locking mechanism that Grim describes is quite common in older revision control systems such as PVCS and RCS (and maybe CVS, I am not sure of its internals).

It works quite well, but if anything goes wrong, such as the server dying in the middle of a transaction, then it will leave the lock file behind. This will prevent anyone writing to the files until it is manually deleted. You could get round this by checking the 'last modified' timestamp on the file and deleting it if it is significantly older than maximum duration of your CGI script.

Another common way to do it would be to have a separate logging program running that you could connect to and send it your logging messages. The program would queue up the messages and write them to the log file one at a time. I believe that both NT and UNIX systems have a system logging process that does this, but they only write to the system log, not to any file.

Or you could use a DB, as you said. It would not need to be a full blown relational database - the BerkleyDB (bsddb) can support transactions if compiled with the right options. See http://pybsddb.sourceforge.net/ for the Python bindings to it.


Dave - The Developers' Coach

Reply With Quote
  #5  
Old April 22nd, 2004, 10:21 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 34 sec
Reputation Power: 0
Thanks for the reply guys. sorry about the double post. I'll check out what you were all talking about.
[confusion] i noticed if i open a text file and then go online and submit to that same file, the info gets written. however if i do the same with a csv file, cgitb tells me it can't do that. obviously because the file is open. is this because its not plain text and csv is an actually program or something[/confusion]

Last edited by caroundw5h : April 23rd, 2004 at 04:33 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > file locking absolute on a website?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway