472,136 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

flock trouble

Seb
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,
seb


#! /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):
Pyro.core.ObjBase.__init__(self)
self.basePath = self.basePath.strip("..")
if not os.path.isdir(self.basePath) or
os.path.islink(self.basePath):
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)
f.close()
return data

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

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)
f.close()

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

daemon.useNameServer(ns)

try:
ns.unregister("fileServer")
except NamingError:
pass

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

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

daemon.requestLoop()
except KeyboardInterrupt:
ns.unregister("fileServer")
print "ctrl + c pressed"

Sep 8 '08 #1
1 1662
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marc | last post: by
reply views Thread by Daniel Brunthaler | last post: by
14 posts views Thread by deko | last post: by
3 posts views Thread by Rex Karz | last post: by
4 posts views Thread by writeson | last post: by
3 posts views Thread by siliconmike | last post: by
reply views Thread by Babu | last post: by
4 posts views Thread by Time Waster | last post: by
2 posts views Thread by xucs007 | last post: by
reply views Thread by leo001 | last post: by

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.