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
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()
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
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
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 # These act as wrappers for processes in the underlying database def read(self): #print("Server trying to read") s=self.db.read(); #print("Server has read fortune:"+s) return(s) def write(self, fortune): # We return the result of self.db.write even though this function has no return statement. # This provides extensibility, since future versions may return some information such as a status code. #print("trying to write fortune:" + fortune) self.db.write(fortune) #print("Server thinks it worked") return("Successfully wrote fortune \""+fortune+"\" to database")
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
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): # return self.db.read() # def write(self, fortune): # self.db.write(fortune)
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()
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"
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 # These act as wrappers for processes in the underlying database def read(self): #print("Server trying to read") s = self.db.read() #print("Server has read fortune:"+s) return (s) def write(self, fortune): # We return the result of self.db.write even though this function has no return statement. # This provides extensibility, since future versions may return some information such as a status code. #print("trying to write fortune:" + fortune) self.db.write(fortune) #print("Server thinks it worked") return ("Successfully wrote fortune \"" + fortune + "\" to database")
def __init__(self, db_file): self.db = Database(db_file) self.rwlock = ReadWriteLock()
dest="fortune", help="Write a new fortune to the database.") parser.add_argument("-i", "--interactive", action="store_true", dest="interactive", default=False, help="Interactive session with the fortune database.") opts = parser.parse_args() # ----------------------------------------------------------------------------- # The main program # ----------------------------------------------------------------------------- # Create the database object db = Database("dbs/fortune.db") if not opts.interactive: # Run in the normal mode if opts.fortune is not None: db.write(opts.fortune) else: print(db.read()) else: # Run in the interactive mode def menu(): print("""\ Choose one of the following commands: r :: read a random fortune from the database, w <FORTUNE> :: write a new fortune into the database,
Client for a fortune database. It reads a random fortune from the database. """ arg_parser = OptionParser(description = description) arg_parser.add_option("-w", "--write", metavar = "FORTUNE", dest = "fortune", help = "Write a new fortune to the database.") arg_parser.add_option("-i", "--interactive", action = "store_true", dest = "interactive", default = False, help = "Interactive session with the fortune database.") opts, args = arg_parser.parse_args() # ------------------------------------------------------------------------------ # The main program # ------------------------------------------------------------------------------ # Create the database object db = Database("dbs/fortune.db") if not opts.interactive: # Run in the normal mode if opts.fortune is not None: db.write(opts.fortune) else: print db.read() else: # Run in the interactive mode def menu(): print ( "Choose one of the following commands:\n" " r :: read a random fortune from the database,\n" " w <FORTUNE> :: write a new fortune into the database,\n"