Ejemplo n.º 1
0
class Server(object):
    """Class that provides synchronous access to the database."""

    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        #
        # Your code here.
        #
        
        self.rwlock.read_acquire()
        result = self.db.read()
        self.rwlock.read_release()
        print(" \n Read to Database called! \n")
        return result
        pass

    def write(self, fortune):
        #
        # Your code here.
        #
        print("WRITE GOT CALLED")
        self.rwlock.write_acquire()
        to_write = '\n'+fortune+'\n%'
        result = self.db.write(to_write)
        self.rwlock.write_release()
        return "wrote:" + fortune
        pass
Ejemplo n.º 2
0
class Server(object):

    """Class that provides synchronous access to the database."""

    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        #
        # Your code here.
        #

        try:
            self.rwlock.read_acquire()
            return self.db.read()
        finally:
            self.rwlock.read_release()

        # end my code

    def write(self, fortune):
        #
        # Your code here.
        #
        try:
            self.rwlock.write_acquire()
            self.db.write(fortune)
        finally:
            self.rwlock.write_release()
class Server(object):

    """Class that provides synchronous access to the database."""

    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        #
        # Your code here.
        #
        self.rwlock.read_acquire()
        result = self.db.read()
        self.rwlock.read_release()
        return result
        pass

    def write(self, fortune):
        #
        # Your code here.
        #
        self.rwlock.write_acquire()
        result = self.db.write(fortune)
        self.rwlock.write_release()
        return result
        pass
Ejemplo n.º 4
0
class Server(object):
    """Class that provides synchronous access to the database."""
    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        """ Reading the database and utilizing the read-lock
        :return result (read fortune from the database) """

        self.rwlock.read_acquire()

        try:
            result = self.db.read()
        finally:
            self.rwlock.read_release()

        return result

    def write(self, fortune):
        """ Writing to the database and utilizing the write-lock
        :return result) """

        self.rwlock.write_acquire()

        try:
            self.db.write(fortune[0])
        finally:
            self.rwlock.write_release()

        return
Ejemplo n.º 5
0
class Server(object):

    """Class that provides synchronous access to the database."""

    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        # Acquire read lock before accessing critical data.
        self.rwlock.read_acquire()
        
        tmp = self.db.read()
        
        # Release lock when finished with critical data.
        self.rwlock.read_release()

        return tmp
        

    def write(self, fortune):
        # Acquire write lock before accessing critical data.
        self.rwlock.write_acquire()

        self.db.write(fortune)

        # Release lock when finished with critical data.
        self.rwlock.write_release()
Ejemplo n.º 6
0
class Server(object):

    """Class that provides synchronous access to the database."""

    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        self.rwlock.read_acquire()
        try:
            result, status = self.db.read(), "success"
        except:
            result, status = Exception, "fail"
        finally:
            self.rwlock.read_release()
            return result, status

    def write(self, fortune):
        self.rwlock.write_acquire()
        try:
            self.db.write(fortune)
            result = "success"
        except:
            result = Exception
        finally:
            self.rwlock.write_release()
            return result
Ejemplo n.º 7
0
class Server(object):
    """Class that provides synchronous access to the database."""
    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods

    def read(self):
        #
        # Your code here.
        #
        # Remember the rule:
        # 1. Open the lock
        # 2. Perform action
        # 3. Close the lock
        # We do this so that if after opening the lock,
        # the database can crash while performing the action
        # So even if that happens, the lock should always be placed at the end
        # Hence the 'finally'
        try:
            self.rwlock.read_acquire()
            fortune = self.db.read()
        finally:
            self.rwlock.read_release()
        return fortune

    def write(self, fortune):
        #
        # Your code here.
        #
        # Remember the rule:
        # 1. Open the lock
        # 2. Perform action
        # 3. Close the lock
        # We do this so that if after opening the lock,
        # the database can crash while performing the action
        # So even if that happens, the lock should always be placed at the end
        # Hence the 'finally'
        try:
            self.rwlock.write_acquire()
            self.db.write(fortune)
        finally:
            self.rwlock.write_release()
Ejemplo n.º 8
0
class Server(object):
    """Class that provides synchronous access to the database."""

    def __init__(self, db_file):
        self.db = Database(db_file)
        self.rwlock = ReadWriteLock()

    # Public methods
    def read(self):
        #
        # Your code here.
        #

        # Acquire the lock before reading, in order to
        # prevent any writing operations during read.
        self.rwlock.read_acquire()
        
        try: 
            result = self.db.read()
        finally:
            # Release lock and return result when we are done.
            self.rwlock.read_release()
        return result

    def write(self, fortune):
        #
        # Your code here
        #
        
        # Acquire the lock when writing, in order for no
        # reading or writing from other threads to perform
        # any operations during this write. Lock is released 
        # afterwards, and default value "NULL" is returned to
        # indicate that everything went as it should.
        self.rwlock.write_acquire()
        try:
            self.db.write(fortune)
        finally:
            self.rwlock.write_release()
        return "NULL"
Ejemplo n.º 9
0
 def __init__(self, db_file):
     self.db = Database(db_file)
     self.rwlock = ReadWriteLock()
 def __init__(self, db_file):
     self.db = Database(db_file)
     self.rwlock = ReadWriteLock()