本文整理汇总了Python中multiprocessing.connection.Listener类的典型用法代码示例。如果您正苦于以下问题:Python Listener类的具体用法?Python Listener怎么用?Python Listener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Listener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
setproctitle('graph4py.rpc')
parser = argparse.ArgumentParser(description='Run Graph4Py graph server')
parser.add_argument(
'--version',
'-v',
action='version',
version=__version__
)
parser.add_argument('host')
parser.add_argument('port', type=int)
parser.add_argument('backend')
parser.add_argument('path')
parser.add_argument('--authkey', '-k', action='store')
parser.add_argument('--graph', '-g', action='store')
args = parser.parse_args()
graph_class = import_object(args.graph) if args.graph else None
database = Graph4Py(args.backend, args.path, args.authkey, graph_class)
def signal_handler(s, frame):
database.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
listener = Listener((args.host, args.port), family='AF_INET')
print 'Running on %s:%s with %s' % (args.host, args.port, database.graph)
while True:
connection = listener.accept()
database.process(connection)
示例2: CountApiServer
class CountApiServer(threading.Thread):
def __init__(self, port, parent):
super(CountApiServer, self).__init__()
self.parent = parent
self.__listener = Listener(('127.0.0.1', port))
def run(self):
conn = self.__listener.accept()
while 1:
if conn.closed:
conn = self.__listener.accept()
try:
msg = conn.recv()
except EOFError:
conn.close()
continue
else:
if msg == 'quit':
conn.close()
break
conn.send(self.parent.key_count)
self.__listener.close()
self.parent.stop()
示例3: rpc
def rpc(self, dest, method, args=None, kwargs=None):
""" Make an RPC call to the named subscriber, expecting a
response. This opens a
:class:`multiprocessing.connection.Listener` and passes the
Listener address to the child as part of the RPC call, so that
the child can connect to the Listener to submit its results.
Listeners are reused when possible to minimize overhead.
"""
try:
listener = self._available_listeners.get_nowait()
self.logger.debug("Reusing existing RPC listener at %s" %
listener.address)
except Empty:
listener = Listener()
self.logger.debug("Created new RPC listener at %s" %
listener.address)
self._blocking_listeners.append(listener)
try:
self._queues[dest].put((listener.address,
(method, args or [], kwargs or dict())))
conn = listener.accept()
self._blocking_listeners.remove(listener)
try:
while not self._terminate.is_set():
if conn.poll(self.poll_wait):
return conn.recv()
finally:
conn.close()
finally:
self._available_listeners.put(listener)
示例4: loop
def loop(self):
listener = Listener(self.address, backlog=5, authkey=self.authkey)
logging.warn('DB server started with %s, listening on %s:%d...',
sys.executable, *self.address)
self.thread.start()
cmd = None
try:
while cmd != 'stop':
try:
conn = listener.accept()
except KeyboardInterrupt:
break
except:
# unauthenticated connection, for instance by a port
# scanner such as the one in manage.py
continue
cmd_ = conn.recv() # a tuple (name, arg1, ... argN)
cmd, args = cmd_[0], cmd_[1:]
if cmd.startswith('@'): # slow command, run in process
cmd = cmd[1:] # strip @
proc = Process(
target=run_command, name=cmd, args=(cmd, args, conn))
proc.start()
logging.warn('Started %s%s in process %d',
cmd, args, proc.pid)
else:
queue.put((conn, cmd, args))
finally:
listener.close()
self.thread.join()
示例5: _serve
def _serve(self, port, path):
data = []
# print 'Server', os.getpid()
address = ('localhost', port)
listener = Listener(address)
loop = True
while loop:
conn = listener.accept()
command, payload = conn.recv()
response_payload = None
if command == 'OPEN':
#print command, payload
path = payload
elif command == 'WRITE':
#print command, payload
data.append(payload)
elif command == 'FETCH':
#print command, payload
response_payload = data
elif command == 'FLUSH':
#print command
json.dump(data, open(path, 'w'))
elif command == 'CLOSE':
#print command
#json.dump(data, open(path, 'w'))
loop = False
conn.send(('OK', response_payload))
conn.close()
listener.close()
示例6: main
def main():
_setproctitle('structurarium.graph')
parser = argparse.ArgumentParser(description='Run Structurarium graph server')
parser.add_argument('--version', '-v', action='version', version=__version__)
parser.add_argument('host')
parser.add_argument('port', type=int)
parser.add_argument('path')
parser.add_argument('--authkey', '-k', action='store')
parser.add_argument(
'--worker',
'-w',
action='store',
type=int,
help='default is set to the number of CPU'
)
args = parser.parse_args()
listener = Listener((args.host, args.port), family='AF_INET')
database = Graph(args.path, authkey=args.authkey)
pool = Pool(processes=args.worker)
print 'Running on %s:%s' % (args.host, args.port)
while True:
connection = listener.accept()
connection = reduce_connection(connection)
# pool.apply_async(connect, [database, connection])
connect(database, connection)
示例7: worker
def worker(address, port, password):
listener = Listener(address, authkey=password)
conn = listener.accept()
print 'connection accepted from', listener.last_accepted
conn.send_bytes('hello')
conn.close()
listener.close()
示例8: objectLogger
def objectLogger(params):
""" Send data (dict, str, ...) into a storage """
mode = params['mode']
maxlen = params['maxlen']
file_format = params['format']
home_dir = params['storage_dir']
server = Listener((params['ip'], int(params['port'])))
storage = PersistentDeque(
home_dir=home_dir,
maxlen=maxlen,
file_format=file_format,
mode=mode,
)
while True:
conn = server.accept()
while True:
try:
data = conn.recv()
except EOFError:
break
if data:
storage.append(data)
else:
storage.sync(id_generator())
storage = PersistentDeque(
home_dir=home_dir,
maxlen=maxlen,
file_format=file_format,
mode=mode,
)
conn.close()
示例9: rpc_server
def rpc_server(handler, address, authkey):
sock = Listener(address, authkey=authkey)
while True:
client = sock.accept()
t = Thread(target=handler.handle_connection, args=(client,))
t.daemon = True
t.start()
示例10: run
def run(self):
listener = Listener(self.address, self.family)
while True:
conn = listener.accept()
t = threading.Thread(target=self._handle_conn, args=(conn,))
t.daemon = True
t.start()
示例11: close
def close(self, blockUntilClosed = True):
'''close may wait 1 second.'''
Listener.close(self)
if self.establish_connection_to_close_lock.acquire(False):
if not blockUntilClosed:
thread_start_new(self._connect_to_pipe_to_close_it, ())
else:
self._connect_to_pipe_to_close_it()
示例12: send
def send(msg):
address = ('localhost', 6000)
listener = Listener(address)
conn = listener.accept()
conn.send(msg)
conn.close()
listener.close()
示例13: run
def run(self):
serv = Listener(self.address,authkey=self.authkey)
while True:
try:
client = serv.accept()
DispatchClientTask(client).start()
except Exception as e:
self.log.info("Error : %s", e, exc_info=True)
示例14: start
def start(self):
address = ('localhost', 6000)
listener = Listener(address)
conn = listener.accept()
while True:
msg = conn.recv()
self.log_id += 1
self.log_file.add_row(LogEntry([self.node_id, self.log_id, msg]))
示例15: _listenThread
def _listenThread(self):
address = ('localhost', self.port) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey=self.pwd)
while True:
conn = listener.accept()
self._printDebug('MultiBus,Listner: connection accepted from' + str(listener.last_accepted))
p = Process(target=self._connThread, args=(conn,))
p.start()