本文整理汇总了Python中multiprocessing.connection.Listener.close方法的典型用法代码示例。如果您正苦于以下问题:Python Listener.close方法的具体用法?Python Listener.close怎么用?Python Listener.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.connection.Listener
的用法示例。
在下文中一共展示了Listener.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CountApiServer
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
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()
示例2: _serve
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
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()
示例3: worker
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
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()
示例4: loop
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
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: send
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def send(msg):
address = ('localhost', 6000)
listener = Listener(address)
conn = listener.accept()
conn.send(msg)
conn.close()
listener.close()
示例6: close
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
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()
示例7: SingleInstanceApp
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
class SingleInstanceApp(BaseJobManager):
def __init__(self, name, session=''):
BaseJobManager.__init__(self)
if sys.platform == 'win32':
self.address = r'\\.\pipe\single_instance_app.'
else:
self.address = '/var/tmp/single_instance_app.pipe'
trans = string.maketrans('\\ ', '__')
self.address += '.'+name.translate(trans)
if session and len(session)>0:
self.address += session.translate(trans)
self.accepting_connection = False
def _accept_connection(self):
while self.accepting_connection:
conn = self.listener.accept()
args = conn.recv()
conn.close()
self.queue_launch_instance(*args)
def try_start_instance(self, *args):
try:
conn = Client(self.address, authkey='secret password')
conn.send(args)
conn.close()
#print "sent my args(%s)"%(', '.join(args))
return False
except:
self.queue_launch_instance(*args)
self.listener = Listener(self.address, authkey='secret password')
self.accepting_connection = True
self.listener_tread = threading.Thread(target=self._accept_connection)
self.listener_tread.daemon = True #because of listener.accept() blocks
self.listener_tread.start()
return True
def stop_accepting(self):
self.accepting_connection = False
self.listener.close()
self.listener_tread.join(0.01)
def queue_launch_instance(self, *args):
fancy_log(sys.stdout, 'queued instance launch with args: '+str(args))
self.add_job(*args)
#override
#def do_the_job(self, *args):
# print 'calls instance with args: ',args
# self.update_job()
def job_is_updated(self, *args, **kwargs):
fancy_log(sys.stdout,'calls instance with args: '+str(args)+'\n kwargs: '+str(kwargs))
pass
示例8: server
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
class server():
def __init__(self, sock=8000, ip='localhost', authkey='secret password', MAX=3):
self.sock = sock
self.ip = ip
self.authkey = authkey
self.connections = []
self.scale = MAX
self.jobs = []
self.outputs = []
self.socket = None
def setScale(self, scale):
self.scale = scale
def addJobs(self, _list):
self.jobs.extend(_list)
def _listen(self):
scale = self.scale
while True:
if scale:
scale -= 1
conn = self.socket.accept()
self.connections.append(conn)
#print 'connection accepted from', conn.last_accepted
else:
break
def start(self):
self.socket = Listener((self.ip, self.sock), authkey=self.authkey)
self._listen()
next = self.scale-1
scheduler = chunks(self.jobs, self.scale)
if len(self.jobs) is 0:
print "Please add some jobs before you start me !!"
return
for each in scheduler:
self.connections[next].send(each)
next -= 1
if next < 0:
break
now = time.time()
for conn in self.connections:
x = conn.recv()
self.outputs.extend(x)
conn.close()
self.socket.close()
t = time.time() - now
X = []
X.append(["time", t])
print tabulate(X, tablefmt='rst')
#print str("Took me " + str(t) + " time")
return self.outputs
示例9: transient_server
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def transient_server(address, authkey, seconds):
class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException('end for listener')
signal.signal(signal.SIGALRM, timeout_handler)
server = Listener(address, authkey=authkey)
try:
yield server
except TimeoutException:
server.close()
示例10: start_serve
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def start_serve(self):
"""start the server"""
while True:
listener = Listener(self.address)
conn = listener.accept()
process = ClientModuleConnect(listener.last_accepted, conn)
process.start()
listener.close()
示例11: run
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def run(self):
address = ("localhost", 6000)
listener = Listener(address, authkey="secret password")
conn = listener.accept()
while True:
command = self.queue.get()
conn.send_bytes(command)
self.queue.task_done()
if command == "quit":
break
conn.close()
listener.close()
示例12: Visualizer
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
class Visualizer(object):
def __init__(self, din = {}):
import plotter
self.dict = {
'plotter': plotter.SubplotAnimation(),
}
self.dict.update(din)
print "[IPC-S] Server process ID ", os.getpid()
self.init_communication()
self.__run__()
def init_communication(self):
print "[IPC-S] Opening Server socket ... ",
address = ('localhost', 6000) # family is deduced to be 'AF_INET'
self.listener = Listener(address)
print "done!"
print "[IPC-S] Waiting Client ... ",
self.conn = self.listener.accept()
print 'connection accepted from', self.listener.last_accepted
def close_communication(self):
print "[IPC-S] Closing interprocess communication ... ",
self.conn.close()
self.listener.close()
print "done!"
def read_msg(self):
return self.conn.recv()
def __run__(self):
while True:
msg = self.read_msg()
# msg format: [os.getpid(), i, time, avg[0], avg[1], avg[2]]
# Process the message
if msg == 'close':
self.close_communication()
break
procID = msg[0]
counter = msg[1]
logTime = msg[2]
avg = msg[3:]
#print os.getpid(), "--- Recived ", msg, " ||| avg ", avg, " ||| counter ", counter
# Plot the data
self.dict['plotter'].draw_frame([logTime, avg[0], avg[1], avg[2]] ) # plots a point every 100 ms
示例13: run_server
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def run_server(address, authkey, mod, q):
serv = Listener(address, authkey =authkey)
while True:
try:
client= serv.accept()
msg= make_work(client, mod, q)
if msg == 'Close': # 关闭监听
serv.close()
return "Close"
else:
client.send(msg)
except Exception:
traceback.print_exc()
serv.close()
示例14: run
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def run(self):
listener = Listener(self.address, authkey=self.auth)
while True:
with listener.accept() as connection:
msg = connection.recv()
if msg == 'exit':
break
else:
if shared.settings['use_indicator']:
GLib.idle_add(getattr(shared.indicator, msg).activate)
else:
GLib.idle_add(getattr(shared.indicator, msg))
listener.close()
示例15: run
# 需要导入模块: from multiprocessing.connection import Listener [as 别名]
# 或者: from multiprocessing.connection.Listener import close [as 别名]
def run(self):
print 'Server setup'
try:
listener = Listener((self.host, self.port), authkey=self.pw)
print 'Server address :',listener.address
conn = listener.accept()
print 'connection accepted'
while True:
msg = conn.recv()
resp = self.dispatch(msg)
conn.send(resp)
except EOFError,IOError:
conn.close()
listener.close()
print "Connection terminated by client"