Exemple #1
0
def get_maestral_proxy(config_name='maestral', fallback=False):
    """
    Returns a Pyro proxy of the a running Maestral instance. If ``fallback`` is
    ``True``, a new instance of Maestral will be returned when the daemon cannot be
    reached.

    :param str config_name: The name of the Maestral configuration to use.
    :param bool fallback: If ``True``, a new instance of Maestral will be returned when
        the daemon cannot be reached. Defaults to ``False``.
    :returns: Pyro proxy of Maestral or a new instance.
    :raises: ``Pyro5.errors.CommunicationError`` if the daemon cannot be reached and
        ``fallback`` is ``False``.
    """

    pid = get_maestral_pid(config_name)

    if pid:
        sock_name = sockpath_for_config(config_name)

        sys.excepthook = Pyro5.errors.excepthook
        maestral_daemon = Proxy(URI.format(config_name, './u:' + sock_name))
        try:
            maestral_daemon._pyroBind()
            return maestral_daemon
        except Pyro5.errors.CommunicationError:
            maestral_daemon._pyroRelease()

    if fallback:
        from maestral.main import Maestral
        m = Maestral(config_name, run=False)
        m.log_handler_stream.setLevel(logging.CRITICAL)
        return m
    else:
        raise Pyro5.errors.CommunicationError
Exemple #2
0
def wait_for_startup(config_name: str, timeout: float = 5) -> None:
    """
    Waits until we can communicate with the maestral daemon for ``config_name``.

    :param config_name: Configuration to connect to.
    :param timeout: Timeout it seconds until we raise an error.
    :raises CommunicationError: if we cannot communicate with the daemon within the
        given timeout.
    """

    sock_name = sockpath_for_config(config_name)
    maestral_daemon = Proxy(URI.format(config_name, "./u:" + sock_name))

    t0 = time.time()

    while True:
        try:
            maestral_daemon._pyroBind()
            return
        except Exception as exc:
            if time.time() - t0 > timeout:
                raise exc
            else:
                time.sleep(0.2)
        finally:
            maestral_daemon._pyroRelease()
Exemple #3
0
def _wait_for_startup(config_name: str, timeout: float = 8) -> Start:
    """Checks if we can communicate with the maestral daemon. Returns ``Start.Ok`` if
    communication succeeds within timeout, ``Start.Failed``  otherwise."""

    sock_name = sockpath_for_config(config_name)
    maestral_daemon = Proxy(URI.format(config_name, "./u:" + sock_name))

    while timeout > 0:
        try:
            maestral_daemon._pyroBind()
            return Start.Ok
        except Exception:
            time.sleep(0.2)
            timeout -= 0.2
        finally:
            maestral_daemon._pyroRelease()

    return Start.Failed
Exemple #4
0
def _check_pyro_communication(config_name, timeout=2):
    """Checks if we can communicate with the maestral daemon. Returns ``Start.Ok`` if
    communication succeeds within timeout, ``Start.Failed``  otherwise."""

    sock_name = sockpath_for_config(config_name)
    maestral_daemon = Proxy(URI.format(config_name, './u:' + sock_name))

    # wait until we can communicate with daemon, timeout after :param:`timeout`
    while timeout > 0:
        try:
            maestral_daemon._pyroBind()
            logger.debug('Successfully communication with daemon')
            return Start.Ok
        except Exception:
            time.sleep(0.2)
            timeout -= 0.2
        finally:
            maestral_daemon._pyroRelease()

    logger.error('Could not communicate with Maestral daemon')
    return Start.Failed
Exemple #5
0
import time
from Pyro5.api import Proxy
import Pyro5.config

uri = input("Uri of benchmark server? ").strip()

print("Timing raw connect speed (no method call)...")
p = Proxy(uri)
p.oneway()
ITERATIONS = 2000
begin = time.time()
for loop in range(ITERATIONS):
    if loop % 500 == 0:
        print(loop)
    p._pyroRelease()
    p._pyroBind()
duration = time.time() - begin
print("%d connections in %.3f sec = %.0f conn/sec" %
      (ITERATIONS, duration, ITERATIONS / duration))
del p

print("Timing proxy creation+connect+methodcall speed...")
ITERATIONS = 2000
begin = time.time()
for loop in range(ITERATIONS):
    if loop % 500 == 0:
        print(loop)
    with Proxy(uri) as p:
        p.oneway()
duration = time.time() - begin
print("%d new proxy calls in %.3f sec = %.0f calls/sec" %
# upgrade the photo printer
uri, meta = ns.lookup("example.resource.photoprinter", return_metadata=True)
meta = set(meta)
meta.discard("performance:slow")
meta.add("performance:fast")
ns.set_metadata("example.resource.photoprinter", meta)

print("\nall FAST printers (after photoprinter upgrade):")
devices = ns.yplookup(meta_all={"resource:printer", "performance:fast"})
for name, uri in devices.items():
    print("   {} -> {}".format(name, uri))

print("\nall resource types:")
devices = ns.yplookup(meta_all={"class:resources.Resource"})
for name, uri in devices.items():
    print("   {} -> {}".format(name, uri))

print("\n\nPYROMETA protocol for easy yellow-pages lookup:\n")
nameserver = Proxy("PYROMETA:class:Pyro5.nameserver.NameServer")
print("Proxy to look up 'any nameserver' via its class metadata:")
print("    ", nameserver)
nameserver._pyroBind()
print("Proxy for 'any namesever' bound to candidate:")
print("    ", nameserver._pyroUri)
printer = Proxy("PYROMETA:resource:printer,performance:slow")
print("Proxy for 'any slow printer':")
print("    ", printer)
print(
    "(this example doesn't actually implement these objects so we leave it at that)"
)
Exemple #7
0
import time
import Pyro5.config
from Pyro5.api import Proxy

Pyro5.config.SERIALIZER = "marshal"

uri = input("Uri of benchmark server? ").strip()
obj = Proxy(uri)
obj._pyroBind()
assert "oneway" in obj._pyroOneway  # make sure this method is indeed marked as @oneway

funcs = [
    lambda: obj.length('Irmen de Jong'), lambda: obj.timestwo(21),
    lambda: obj.bigreply(),
    lambda: obj.manyargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
    lambda: obj.noreply([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]),
    lambda: obj.noreply(None), lambda: obj.varargs('een', 2, (3, ), [4]),
    lambda: obj.keywords(arg1='zork'), lambda: obj.echo('een', 2, (3, ), [4]),
    lambda: obj.echo({
        "aap": 42,
        "noot": 99,
        "mies": 987654
    }), lambda: obj.bigarg('Argument' * 50),
    lambda: obj.oneway('stringetje', 432423434, 9.8765432)
]

print('-------- BENCHMARK REMOTE OBJECT ---------')
begin = time.time()
iters = 1000
print("warmup...")
for _ in range(iters):
Exemple #8
0
    print("Servertype is multiplex. The calls will need to get in line.")
    print(
        "The time this will take is 5 seconds (every thread takes 1 second sequentially)."
    )
    print(
        "You will see that the requests are handled by a single server thread."
    )
else:
    print("Unknown servertype")


def func(uri):
    # This will run in a thread. Create a proxy just for this thread:
    with Proxy(uri) as p:
        processed = p.delay()
        print("[ thread %s called delay, processed by: %s ]  " %
              (threading.current_thread().getName(), processed))


serv._pyroBind()  # simplify the uri
threads = []
for i in range(5):
    t = threading.Thread(target=func, args=[serv._pyroUri])
    t.setDaemon(True)
    threads.append(t)
    t.start()
print("Waiting for threads to finish:")
for t in threads:
    t.join()
print("Done. Number of completed calls in the server: %d" % serv.getcount())