473,396 Members | 1,693 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,396 software developers and data experts.

flock trouble

I'm trying to implement a file server using the code below. However
the locking doesn't work. I can delete while put'ing a file.

Anyone got an idea about why?

best regards,

#! /usr/bin/env python

import Pyro.core, Pyro.naming
from Pyro.errors import PyroError, NamingError
import sys
import urllib
import os
import fcntl

class fileServer(Pyro.core.ObjBase):
basePath = "/home/snot/diku/dist/opg2/files_to_serve"

def __init__(self):
self.basePath = self.basePath.strip("..")
if not os.path.isdir(self.basePath) or
raise "invalid path"

def get(self, uri):
f = open(self.basePath + uri, 'r+')
fcntl.flock(f, fcntl.LOCK_SH)
data = f.read()
fcntl.flock(f, fcntl.LOCK_UN)
return data

def put(self, uri, payload):
f = open(self.basePath + urllib.unquote_plus(uri), 'w')
fcntl.flock(f, fcntl.LOCK_EX)
fcntl.flock(f, fcntl.LOCK_UN)

def delete(self, uri):
f = open(self.basePath + urllib.unquote_plus(uri), 'w')
fcntl.flock(f, fcntl.LOCK_EX)
os.unlink(self.basePath + uri)
fcntl.flock(f, fcntl.LOCK_UN)

daemon = Pyro.core.Daemon()
locator = Pyro.naming.NameServerLocator()
print 'Searching for Name Server...'
ns = locator.getNS()
except Pyro.errors.PyroError, message:
print message


except NamingError:

uri = daemon.connect(fileServer(), "fileServer")

print "The daemon runs on port:", daemon.port
print "The object's uri is:", uri

except KeyboardInterrupt:
print "ctrl + c pressed"

Sep 8 '08 #1
1 1731
On 2008-09-08, Seb <se******************@gmail.comwrote:
I'm trying to implement a file server using the code below. However
the locking doesn't work. I can delete while put'ing a file.

Anyone got an idea about why?
[code snipped]

As far as I understand you are writing some kind of server. What isn't clear
is whether the daemon is using threading or forking to handle multiple requests.

If I remember correctly, flock doesn't work well with multiple threads.

Antoon Pardon
Sep 26 '08 #2

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

Similar topics

by: Marc | last post by:
Hello, when using flock() I get a permission denied error: Warning: fopen("<filename>", "r+") - Permission denied in <pathtofile> on line 7 I do this: $fileToOpen=substr($PHP_SELF,...
by: Daniel Brunthaler | last post by:
hi, i've a problem with file locking. seems that flock() and gzopen() do not work together. i get the error (or warning) message ... Warning: flock(): cannot represent a stream of type ZLIB...
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
by: Rex Karz | last post by:
Newbie here. I interpret the fine print at http://us2.php.net/manual/en/function.flock.php to mean that flock() does not work where the file being locked is on an NFS filesystem. The...
by: writeson | last post by:
Hi all, I've got a PHP program that I've added flock() to in order to protect multiple scripts trying to write to the same file. After I added the flock() to the code the performance of the code...
by: siliconmike | last post by:
I've scoured over manual page for flock on http://in2.php.net/flock but I can't understand a few things: 1. will flock wait until a different process releases the lock ? 2. please clarify this...
by: Babu | last post by:
Hi, I have an application that maintains its configuration file. This file is read frequently but written to very rarely. There may be several processes attempting to read and write to the config...
by: Time Waster | last post by:
Is this a stupid use of flock: FILE *fp=fopen(SOME_FILE_CONSTANT,"r+"); flock(fileno(fp),LOCK_EX); something important here, including reads and a write to fp flock(fileno(fp),LOCK_UN);...
by: xucs007 | last post by:
I ran following 2 programs (lock1, lock2) at almost same time, to write either "123456", or "222" to file "aaa" at the same time. But I often just got "222456" in "aaa" . Is this a bug of python...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.