/usr/lib/python3/dist-packages/pyutilib/pyro/client.py is in python3-pyutilib 5.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | # _________________________________________________________________________
#
# PyUtilib: A Python utility library.
# Copyright (c) 2008 Sandia Corporation.
# This software is distributed under the BSD License.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# _________________________________________________________________________
__all__ = ['Client']
import sys
import os, socket
import time
from six import iteritems
import pyutilib.pyro.util
from pyutilib.pyro import get_nameserver, using_pyro3, using_pyro4
from pyutilib.pyro import Pyro as _pyro
if sys.version_info >= (3,0):
xrange = range
import queue as Queue
else:
import Queue
class Client(object):
def __init__(self,
group=":PyUtilibServer",
type=None,
host=None,
port=None,
num_dispatcher_tries=30,
caller_name="Client",
dispatcher=None):
if _pyro is None:
raise ImportError("Pyro or Pyro4 is not available")
self.type=type
self.id = 0
# Deprecated in Pyro3
# Removed in Pyro4
if using_pyro3:
_pyro.core.initClient()
self.ns = None
self.CLIENTNAME = "%d@%s" % (os.getpid(), socket.gethostname())
self.dispatcher = None
if dispatcher is None:
self.ns = get_nameserver(host=host, port=port, caller_name=caller_name)
if self.ns is None:
raise RuntimeError("Client failed to locate Pyro name "
"server on the network!")
print('Client attempting to find Pyro dispatcher object...')
self.URI = None
cumulative_sleep_time = 0.0
for i in xrange(0,num_dispatcher_tries):
dispatchers = pyutilib.pyro.util.get_dispatchers(
ns = self.ns)
for (name, uri) in dispatchers:
self.URI = uri
print("Dispatcher Object URI: "+str(self.URI))
break
if self.URI is not None:
break
sleep_interval = 10.0
print("Client failed to find dispatcher object from name "
"server after %d attempts and %5.2f seconds - trying "
"again in %5.2f seconds."
% (i+1,cumulative_sleep_time,sleep_interval))
time.sleep(sleep_interval)
cumulative_sleep_time += sleep_interval
if self.URI is None:
print('Client could not find dispatcher object - giving up')
raise SystemExit
# connect to the dispatcher
if using_pyro3:
self.dispatcher = _pyro.core.getProxyForURI(self.URI)
else:
self.dispatcher = _pyro.Proxy(self.URI)
print("Connection to dispatch server established after %d "
"attempts and %5.2f seconds - this is client: %s"
% (i+1, cumulative_sleep_time, self.CLIENTNAME))
# There is no need to retain the proxy connection to the
# nameserver, so free up resources on the nameserver thread
if using_pyro4:
self.ns._pyroRelease()
else:
self.ns._release()
else:
assert port is None
assert host is None
self.dispatcher = dispatcher
if using_pyro4:
self.URI = self.dispatcher._pyroUri
else:
self.URI = self.dispatcher.URI
print('Client assigned dispatcher with URI=%s'
% (self.URI))
def close(self):
if self.dispatcher is not None:
if using_pyro4:
self.dispatcher._pyroRelease()
else:
self.dispatcher._release()
def clear_queue(self, override_type=None, verbose=False):
task_type = override_type if (override_type is not None) else self.type
if verbose:
print("Clearing all tasks and results for "
"type="+str(task_type))
self.dispatcher.clear_queue(type=task_type)
def add_tasks(self, tasks, verbose=False):
for task_type in tasks:
for task in tasks[task_type]:
if task['id'] is None:
self.id += 1
task['client'] = self.CLIENTNAME
if verbose:
print("Adding task "+str(task['id'])+" to dispatcher "
"queue with type="+str(task_type)+" - in bulk")
self.dispatcher.add_tasks(tasks)
def add_task(self, task, override_type=None, verbose=False):
task_type = override_type if (override_type is not None) else self.type
if task['id'] is None:
self.id += 1
task['client'] = self.CLIENTNAME
if verbose:
print("Adding task "+str(task['id'])+" to dispatcher "
"queue with type="+str(task_type)+" - individually")
self.dispatcher.add_task(task, type=task_type)
def get_result(self, override_type=None, block=True, timeout=5):
task_type = override_type if (override_type is not None) else self.type
return self.dispatcher.get_result(type=task_type, block=block, timeout=timeout)
def get_results(self, override_type=None, block=True, timeout=5):
task_type = override_type if (override_type is not None) else self.type
return self.dispatcher.get_results([(task_type, block, timeout)])[task_type]
def get_results_all_queues(self):
return self.dispatcher.get_results_all_queues()
def num_tasks(self, override_type=None):
task_type = override_type if (override_type is not None) else self.type
return self.dispatcher.num_tasks(type=task_type)
def num_results(self, override_type=None):
task_type = override_type if (override_type is not None) else self.type
return self.dispatcher.num_results(type=override_type)
def queues_with_results(self):
return self.dispatcher.queues_with_results()
|