flock trouble

Discussion in 'Python' started by Seb, Sep 8, 2008.

  1. Seb

    Seb Guest

    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"
     
    Seb, Sep 8, 2008
    #1
    1. Advertising

  2. On 2008-09-08, Seb <> wrote:
    > 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
     
    Antoon Pardon, Sep 22, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Babu
    Replies:
    0
    Views:
    526
  2. thakadu
    Replies:
    3
    Views:
    523
    thakadu
    Nov 4, 2005
  3. Ckid

    flock() working mechanism..

    Ckid, Mar 7, 2005, in forum: C Programming
    Replies:
    1
    Views:
    351
    Peter Nilsson
    Mar 7, 2005
  4. Time Waster

    flock usage

    Time Waster, Aug 31, 2007, in forum: C Programming
    Replies:
    4
    Views:
    5,572
  5. Replies:
    1
    Views:
    1,838
    Jens Henrik Leonhard Jensen
    Jun 22, 2008
Loading...

Share This Page