本文整理汇总了Python中Pyro4.locateNS方法的典型用法代码示例。如果您正苦于以下问题:Python Pyro4.locateNS方法的具体用法?Python Pyro4.locateNS怎么用?Python Pyro4.locateNS使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Pyro4
的用法示例。
在下文中一共展示了Pyro4.locateNS方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getNS
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def getNS():
"""
Return a Pyro name server proxy. If there is no name server running,
start one on 0.0.0.0 (all interfaces), as a background process.
"""
import Pyro4
try:
return Pyro4.locateNS()
except Pyro4.errors.NamingError:
logger.info("Pyro name server not found; starting a new one")
os.system("python -m Pyro4.naming -n 0.0.0.0 &")
# TODO: spawn a proper daemon ala http://code.activestate.com/recipes/278731/ ?
# like this, if there's an error somewhere, we'll never know... (and the loop
# below will block). And it probably doesn't work on windows, either.
while True:
try:
return Pyro4.locateNS()
except:
pass
示例2: getNS
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def getNS():
"""
Return a Pyro name server proxy. If there is no name server running,
start one on 0.0.0.0 (all interfaces), as a background process.
"""
import Pyro4
try:
return Pyro4.locateNS()
except Pyro4.errors.NamingError:
logger.info("Pyro name server not found; starting a new one")
os.system("python -m Pyro4.naming -n 0.0.0.0 &")
# TODO: spawn a proper daemon ala http://code.activestate.com/recipes/278731/ ?
# like this, if there's an error somewhere, we'll never know... (and the loop
# below will block). And it probably doesn't work on windows, either.
while True:
try:
return Pyro4.locateNS()
except:
pass
示例3: get_my_ip
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def get_my_ip():
"""
Try to obtain our external ip (from the pyro nameserver's point of view)
This tries to sidestep the issue of bogus `/etc/hosts` entries and other
local misconfigurations, which often mess up hostname resolution.
If all else fails, fall back to simple `socket.gethostbyname()` lookup.
"""
import socket
try:
import Pyro4
# we know the nameserver must exist, so use it as our anchor point
ns = Pyro4.naming.locateNS()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((ns._pyroUri.host, ns._pyroUri.port))
result, port = s.getsockname()
except:
try:
# see what ifconfig says about our default interface
import commands
result = commands.getoutput("ifconfig").split("\n")[1].split()[1][5:]
if len(result.split('.')) != 4:
raise Exception()
except:
# give up, leave the resolution to gethostbyname
result = socket.gethostbyname(socket.gethostname())
return result
示例4: register_server
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def register_server(serverobject, host=None, port=0, unixsocket=None, nathost=None, natport=None):
"""Regiseter the server with Pycopia asyncio event handler."""
host = host or Pyro4.config.HOST or Pyro4.socketutil.getIpAddress(socket.getfqdn())
pyrodaemon = Pyro4.Daemon(host=host, port=port,
unixsocket=unixsocket, nathost=nathost, natport=natport)
uri = pyrodaemon.register(serverobject)
ns=Pyro4.locateNS()
ns.register("{}:{}".format(serverobject.__class__.__name__, socket.getfqdn()), uri)
p = PyroAsyncAdapter(pyrodaemon)
asyncio.poller.register(p)
return p
示例5: get_remote
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def get_remote(hostname, servicename=None):
"""Find and return a client (proxy) give the fully qualified host name and optional servicename.
"""
if servicename:
patt = "{}:{}".format(servicename, hostname)
else:
patt = hostname
ns = Pyro4.locateNS()
slist = ns.list(prefix=patt)
if slist:
return Pyro4.Proxy(slist.popitem()[1])
else:
raise NameNotFoundError("Service name {!r} not found.".format(patt))
示例6: locate_nameserver
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def locate_nameserver():
return Pyro4.locateNS()
示例7: run
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def run(self):
with self.discover_cond:
t1 = threading.Thread(target=self.discover_workers, name='discover_workers')
t1.start()
self.logger.info('DISPATCHER: started the \'discover_worker\' thread')
t2 = threading.Thread(target=self.job_runner, name='job_runner')
t2.start()
self.logger.info('DISPATCHER: started the \'job_runner\' thread')
self.pyro_daemon = Pyro4.core.Daemon(host=self.host)
with Pyro4.locateNS(host=self.nameserver, port=self.nameserver_port) as ns:
uri = self.pyro_daemon.register(self, self.pyro_id)
ns.register(self.pyro_id, uri)
self.logger.info("DISPATCHER: Pyro daemon running on %s"%(self.pyro_daemon.locationStr))
self.pyro_daemon.requestLoop()
with self.discover_cond:
self.shutdown_all_threads = True
self.logger.info('DISPATCHER: Dispatcher shutting down')
self.runner_cond.notify_all()
self.discover_cond.notify_all()
with Pyro4.locateNS(self.nameserver, port=self.nameserver_port) as ns:
ns.remove(self.pyro_id)
t1.join()
self.logger.debug('DISPATCHER: \'discover_worker\' thread exited')
t2.join()
self.logger.debug('DISPATCHER: \'job_runner\' thread exited')
self.logger.info('DISPATCHER: shut down complete')
示例8: __init__
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def __init__(self, scan=True, use_pyro_ns=False, bind_localhost=False):
self.mdb = MusicFileDatabase(scan_changes=scan)
host = "localhost" if bind_localhost else Pyro4.socketutil.getIpAddress(None, workaround127=True)
self.pyro_daemon = Pyro4.Daemon(host=host, port=0 if use_pyro_ns else BACKEND_PORT)
self.pyro_uri = self.pyro_daemon.register(JukeboxBackendRemoting, "jukebox.backend")
if use_pyro_ns:
with Pyro4.locateNS() as ns:
ns.register("jukebox.backend", self.pyro_uri)
self.cli = JukeboxBackendCli(self.mdb, self.pyro_uri)
示例9: locate_ns
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def locate_ns(nsaddr, timeout=3.0):
"""
Locate a name server to ensure it actually exists.
Parameters
----------
nsaddr : SocketAddress
The address where the name server should be up and running.
timeout : float
Timeout in seconds before aborting location.
Returns
-------
nsaddr
The address where the name server was located.
Raises
------
NamingError
If the name server could not be located.
"""
host, port = address_to_host_port(nsaddr)
time0 = time.time()
while True:
try:
Pyro4.locateNS(host, port)
return nsaddr
except NamingError:
if time.time() - time0 < timeout:
time.sleep(0.1)
continue
raise TimeoutError('Could not locate the name server!')
示例10: get_my_ip
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def get_my_ip():
"""
Try to obtain our external ip (from the pyro nameserver's point of view)
This tries to sidestep the issue of bogus `/etc/hosts` entries and other
local misconfigurations, which often mess up hostname resolution.
If all else fails, fall back to simple `socket.gethostbyname()` lookup.
"""
import socket
try:
import Pyro4
# we know the nameserver must exist, so use it as our anchor point
ns = Pyro4.naming.locateNS()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((ns._pyroUri.host, ns._pyroUri.port))
result, port = s.getsockname()
except:
try:
# see what ifconfig says about our default interface
import commands
result = commands.getoutput("ifconfig").split("\n")[1].split()[1][5:]
if len(result.split('.')) != 4:
raise Exception()
except:
# give up, leave the resolution to gethostbyname
result = socket.gethostbyname(socket.gethostname())
return result
示例11: startServer
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def startServer():
server = Server()
# make a Pyro daemon
daemon = Pyro4.Daemon()
# locate the name server running
ns = Pyro4.locateNS()
# register the server as a Pyro object
uri = daemon.register(server)
# register the object with a name in the name server
ns.register("server", uri)
# print the uri so we can use it in the client later
print("Ready. Object uri =", uri)
# start the event loop of the server to wait for calls
daemon.requestLoop()
开发者ID:PacktPublishing,项目名称:Python-Parallel-Programming-Cookbook-Second-Edition,代码行数:16,代码来源:pyro_server.py
示例12: _run
# 需要导入模块: import Pyro4 [as 别名]
# 或者: from Pyro4 import locateNS [as 别名]
def _run(self):
# initial ping to the dispatcher to register the worker
try:
with Pyro4.locateNS(host=self.nameserver, port=self.nameserver_port) as ns:
self.logger.debug('WORKER: Connected to nameserver %s'%(str(ns)))
dispatchers = ns.list(prefix="hpbandster.run_%s.dispatcher"%self.run_id)
except Pyro4.errors.NamingError:
if self.thread is None:
raise RuntimeError('No nameserver found. Make sure the nameserver is running at that the host (%s) and port (%s) are correct'%(self.nameserver, self.nameserver_port))
else:
self.logger.error('No nameserver found. Make sure the nameserver is running at that the host (%s) and port (%s) are correct'%(self.nameserver, self.nameserver_port))
exit(1)
except:
raise
for dn, uri in dispatchers.items():
try:
self.logger.debug('WORKER: found dispatcher %s'%dn)
with Pyro4.Proxy(uri) as dispatcher_proxy:
dispatcher_proxy.trigger_discover_worker()
except Pyro4.errors.CommunicationError:
self.logger.debug('WORKER: Dispatcher did not respond. Waiting for one to initiate contact.')
pass
except:
raise
if len(dispatchers) == 0:
self.logger.debug('WORKER: No dispatcher found. Waiting for one to initiate contact.')
self.logger.info('WORKER: start listening for jobs')
self.pyro_daemon = Pyro4.core.Daemon(host=self.host)
with Pyro4.locateNS(self.nameserver, port=self.nameserver_port) as ns:
uri = self.pyro_daemon.register(self, self.worker_id)
ns.register(self.worker_id, uri)
self.pyro_daemon.requestLoop()
with Pyro4.locateNS(self.nameserver, port=self.nameserver_port) as ns:
ns.remove(self.worker_id)