def __init__(self, addr): self.allow_reuse_address = 1 self.hostname, self.port = addr TCPServer.__init__(self, addr, ServerRequestHandler) # shared state self.data = dict() # set server defaults self.data = { u'started_at' : datetime.now(), u'port' : self.port, u'hostname' : Config.hostname, u'running' : True } # create a shared Data object self.manager = DataManager(address=('', self.port + 1), authkey=Config.authkey) # "Private" methods ('__'-prefixed) are *not* exported out of # the manager by default. This includes stuff to make dict work # minimally. See # http://docs.python.org/library/multiprocessing.html # # Upshot is we need to explicitly name the exposed functions: DataManager.register('get_data', callable=lambda: self.data, exposed=('__str__', '__delitem__', '__getitem__', '__setitem__')) self.manager.start() self.manager.connect() self.data = self.manager.get_data()
def __init__(self, addr): self.allow_reuse_address = 1 self.hostname, self.port = addr TCPServer.__init__(self, addr, ServerRequestHandler) # shared state self.data = dict() # set server defaults self.data = { u'started_at': datetime.now(), u'port': self.port, u'hostname': Config.hostname, u'running': True } # create a shared Data object self.manager = DataManager(address=('', self.port + 1), authkey=Config.authkey) # "Private" methods ('__'-prefixed) are *not* exported out of # the manager by default. This includes stuff to make dict work # minimally. See # http://docs.python.org/library/multiprocessing.html # # Upshot is we need to explicitly name the exposed functions: DataManager.register('get_data', callable=lambda: self.data, exposed=('__str__', '__delitem__', '__getitem__', '__setitem__')) self.manager.start() self.manager.connect() self.data = self.manager.get_data()
class Server(HTTPServer): def __init__(self, addr): self.allow_reuse_address = 1 self.hostname, self.port = addr TCPServer.__init__(self, addr, ServerRequestHandler) # shared state self.data = dict() # set server defaults self.data = { 'started_at' : datetime.now(), 'port' : self.port, 'hostname' : Config.hostname, 'running' : True } # create a shared Data object self.manager = DataManager(address=('', self.port + 1), authkey=Config.authkey) # "Private" methods ('__'-prefixed) are *not* exported out of # the manager by default. This includes stuff to make dict work # minimally. See # http://docs.python.org/library/multiprocessing.html # # Upshot is we need to explicitly name the exposed functions: DataManager.register('get_data', callable=lambda: self.data, exposed=('__str__', '__delitem__', '__getitem__', '__setitem__')) self.manager.start() self.manager.connect() self.data = self.manager.get_data() log.info("Bloops and bleeps at http://%s:%s" % self.server_address) self.serve_forever() self.cleanup() def serve_forever(self): while self.data['running']: print "handling requests" self.handle_request() print "done handling requests (server got message)" def cleanup(self): log.debug("cleaning up") self.manager.shutdown() self.server_close()
def set_data(self): try: # get data from manager (see lib/server.py) DataManager.register('get_data') # manager port is one higher than listen port manager = DataManager(address=(Config.hostname, self.port + 1), authkey=Config.authkey) manager.connect() except socket_error as serr: import errno if serr.errno != errno.ECONNREFUSED: raise serr log.error("Connection refused.") sys.exit() self.data = manager.get_data() playlist_data = None try: playlist_data = self.data['playlist'] except KeyError: playlist_data = PlaylistFile.read() if playlist_data is None: playlist_data = PlaylistFile.read() # set default playlist data default_data = { u'playlist': playlist_data, u'continue': False, u'repeat': False, u'shuffle': False, u'status': u'stopped', u'index': 0, u'song': None, u'skip': False, u'sum_bytes': 0, u'progress': 0, u'elapsed': 0, } for k, v in default_data.items(): try: if self.data[k] is None: self.data[k] = default_data[k] except KeyError: self.data[k] = default_data[k]
def set_data(self): try: # get data from manager (see lib/server.py) DataManager.register('get_data') # manager port is one higher than listen port manager = DataManager(address=(Config.hostname, self.port + 1), authkey=Config.authkey) manager.connect() except socket_error as serr: import errno if serr.errno != errno.ECONNREFUSED: raise serr log.error("Connection refused.") sys.exit() self.data = manager.get_data() playlist_data = None try: playlist_data = self.data['playlist'] except KeyError: playlist_data = PlaylistFile.read() if playlist_data is None: playlist_data = PlaylistFile.read() # set default playlist data default_data = { u'playlist' : playlist_data, u'continue' : False, u'repeat' : False, u'shuffle' : False, u'status' : u'stopped', u'index' : 0, u'song' : None, u'skip' : False, u'sum_bytes' : 0, u'progress' : 0, u'elapsed' : 0, } for k, v in default_data.items(): try: if self.data[k] is None: self.data[k] = default_data[k] except KeyError: self.data[k] = default_data[k]
""" test for all nlp """ from lib.data import DataManager DataManager.get_all_data('/Users/gwonchan-u/Downloads')