Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
multiprocessing.managers.Server Class Reference
Inheritance diagram for multiprocessing.managers.Server:
Inheritance graph
[legend]
Collaboration diagram for multiprocessing.managers.Server:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def serve_forever
def handle_request
def serve_client
def fallback_getvalue
def fallback_str
def fallback_repr
def dummy
def debug_info
def number_of_objects
def shutdown
def create
def get_methods
def accept_connection
def incref
def decref

Public Attributes

 registry
 authkey
 listener
 address
 id_to_obj
 id_to_refcount
 mutex
 stop
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

list public
dictionary fallback_mapping

Detailed Description

Server class which runs in a process controlled by a manager object

Definition at line 151 of file managers.py.


Constructor & Destructor Documentation

def multiprocessing.managers.Server.__init__ (   self,
  registry,
  address,
  authkey,
  serializer 
)

Definition at line 158 of file managers.py.

00158 
00159     def __init__(self, registry, address, authkey, serializer):
00160         assert isinstance(authkey, bytes)
00161         self.registry = registry
00162         self.authkey = AuthenticationString(authkey)
00163         Listener, Client = listener_client[serializer]
00164 
00165         # do authentication later
00166         self.listener = Listener(address=address, backlog=5)
00167         self.address = self.listener.address
00168 
00169         self.id_to_obj = {'0': (None, ())}
00170         self.id_to_refcount = {}
00171         self.mutex = threading.RLock()
00172         self.stop = 0

Here is the caller graph for this function:


Member Function Documentation

def multiprocessing.managers.Server.accept_connection (   self,
  c,
  name 
)
Spawn a new thread to serve this connection

Definition at line 420 of file managers.py.

00420 
00421     def accept_connection(self, c, name):
00422         '''
00423         Spawn a new thread to serve this connection
00424         '''
00425         threading.current_thread().name = name
00426         c.send(('#RETURN', None))
00427         self.serve_client(c)

Here is the call graph for this function:

def multiprocessing.managers.Server.create (   self,
  c,
  typeid,
  args,
  kwds 
)
Create a new shared object and return its id

Definition at line 376 of file managers.py.

00376 
00377     def create(self, c, typeid, *args, **kwds):
00378         '''
00379         Create a new shared object and return its id
00380         '''
00381         self.mutex.acquire()
00382         try:
00383             callable, exposed, method_to_typeid, proxytype = \
00384                       self.registry[typeid]
00385 
00386             if callable is None:
00387                 assert len(args) == 1 and not kwds
00388                 obj = args[0]
00389             else:
00390                 obj = callable(*args, **kwds)
00391 
00392             if exposed is None:
00393                 exposed = public_methods(obj)
00394             if method_to_typeid is not None:
00395                 assert type(method_to_typeid) is dict
00396                 exposed = list(exposed) + list(method_to_typeid)
00397 
00398             ident = '%x' % id(obj)  # convert to string because xmlrpclib
00399                                     # only has 32 bit signed integers
00400             util.debug('%r callable returned object with id %r', typeid, ident)
00401 
00402             self.id_to_obj[ident] = (obj, set(exposed), method_to_typeid)
00403             if ident not in self.id_to_refcount:
00404                 self.id_to_refcount[ident] = 0
00405             # increment the reference count immediately, to avoid
00406             # this object being garbage collected before a Proxy
00407             # object for it can be created.  The caller of create()
00408             # is responsible for doing a decref once the Proxy object
00409             # has been created.
00410             self.incref(c, ident)
00411             return ident, tuple(exposed)
00412         finally:
00413             self.mutex.release()

Here is the call graph for this function:

Here is the caller graph for this function:

Return some info --- useful to spot problems with refcounting

Definition at line 320 of file managers.py.

00320 
00321     def debug_info(self, c):
00322         '''
00323         Return some info --- useful to spot problems with refcounting
00324         '''
00325         self.mutex.acquire()
00326         try:
00327             result = []
00328             keys = list(self.id_to_obj.keys())
00329             keys.sort()
00330             for ident in keys:
00331                 if ident != '0':
00332                     result.append('  %s:       refcount=%s\n    %s' %
00333                                   (ident, self.id_to_refcount[ident],
00334                                    str(self.id_to_obj[ident][0])[:75]))
00335             return '\n'.join(result)
00336         finally:
00337             self.mutex.release()

def multiprocessing.managers.Server.decref (   self,
  c,
  ident 
)

Definition at line 435 of file managers.py.

00435 
00436     def decref(self, c, ident):
00437         self.mutex.acquire()
00438         try:
00439             assert self.id_to_refcount[ident] >= 1
00440             self.id_to_refcount[ident] -= 1
00441             if self.id_to_refcount[ident] == 0:
00442                 del self.id_to_obj[ident], self.id_to_refcount[ident]
00443                 util.debug('disposing of obj with id %r', ident)
00444         finally:
00445             self.mutex.release()
00446 
00447 #
00448 # Class to represent state of a manager
00449 #

def multiprocessing.managers.Server.dummy (   self,
  c 
)

Definition at line 317 of file managers.py.

00317 
00318     def dummy(self, c):
00319         pass

Here is the caller graph for this function:

def multiprocessing.managers.Server.fallback_getvalue (   self,
  conn,
  ident,
  obj 
)

Definition at line 302 of file managers.py.

00302 
00303     def fallback_getvalue(self, conn, ident, obj):
00304         return obj

def multiprocessing.managers.Server.fallback_repr (   self,
  conn,
  ident,
  obj 
)

Definition at line 308 of file managers.py.

00308 
00309     def fallback_repr(self, conn, ident, obj):
00310         return repr(obj)

def multiprocessing.managers.Server.fallback_str (   self,
  conn,
  ident,
  obj 
)

Definition at line 305 of file managers.py.

00305 
00306     def fallback_str(self, conn, ident, obj):
00307         return str(obj)

def multiprocessing.managers.Server.get_methods (   self,
  c,
  token 
)
Return the methods of the shared object indicated by token

Definition at line 414 of file managers.py.

00414 
00415     def get_methods(self, c, token):
00416         '''
00417         Return the methods of the shared object indicated by token
00418         '''
00419         return tuple(self.id_to_obj[token.id][1])

Handle a new connection

Definition at line 194 of file managers.py.

00194 
00195     def handle_request(self, c):
00196         '''
00197         Handle a new connection
00198         '''
00199         funcname = result = request = None
00200         try:
00201             connection.deliver_challenge(c, self.authkey)
00202             connection.answer_challenge(c, self.authkey)
00203             request = c.recv()
00204             ignore, funcname, args, kwds = request
00205             assert funcname in self.public, '%r unrecognized' % funcname
00206             func = getattr(self, funcname)
00207         except Exception:
00208             msg = ('#TRACEBACK', format_exc())
00209         else:
00210             try:
00211                 result = func(c, *args, **kwds)
00212             except Exception:
00213                 msg = ('#TRACEBACK', format_exc())
00214             else:
00215                 msg = ('#RETURN', result)
00216         try:
00217             c.send(msg)
00218         except Exception as e:
00219             try:
00220                 c.send(('#TRACEBACK', format_exc()))
00221             except Exception:
00222                 pass
00223             util.info('Failure to send message: %r', msg)
00224             util.info(' ... request was %r', request)
00225             util.info(' ... exception was %r', e)
00226 
00227         c.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def multiprocessing.managers.Server.incref (   self,
  c,
  ident 
)

Definition at line 428 of file managers.py.

00428 
00429     def incref(self, c, ident):
00430         self.mutex.acquire()
00431         try:
00432             self.id_to_refcount[ident] += 1
00433         finally:
00434             self.mutex.release()

Here is the caller graph for this function:

Number of shared objects

Definition at line 338 of file managers.py.

00338 
00339     def number_of_objects(self, c):
00340         '''
00341         Number of shared objects
00342         '''
00343         return len(self.id_to_obj) - 1      # don't count ident='0'

Handle requests from the proxies in a particular process/thread

Definition at line 228 of file managers.py.

00228 
00229     def serve_client(self, conn):
00230         '''
00231         Handle requests from the proxies in a particular process/thread
00232         '''
00233         util.debug('starting server thread to service %r',
00234                    threading.current_thread().name)
00235 
00236         recv = conn.recv
00237         send = conn.send
00238         id_to_obj = self.id_to_obj
00239 
00240         while not self.stop:
00241 
00242             try:
00243                 methodname = obj = None
00244                 request = recv()
00245                 ident, methodname, args, kwds = request
00246                 obj, exposed, gettypeid = id_to_obj[ident]
00247 
00248                 if methodname not in exposed:
00249                     raise AttributeError(
00250                         'method %r of %r object is not in exposed=%r' %
00251                         (methodname, type(obj), exposed)
00252                         )
00253 
00254                 function = getattr(obj, methodname)
00255 
00256                 try:
00257                     res = function(*args, **kwds)
00258                 except Exception as e:
00259                     msg = ('#ERROR', e)
00260                 else:
00261                     typeid = gettypeid and gettypeid.get(methodname, None)
00262                     if typeid:
00263                         rident, rexposed = self.create(conn, typeid, res)
00264                         token = Token(typeid, self.address, rident)
00265                         msg = ('#PROXY', (rexposed, token))
00266                     else:
00267                         msg = ('#RETURN', res)
00268 
00269             except AttributeError:
00270                 if methodname is None:
00271                     msg = ('#TRACEBACK', format_exc())
00272                 else:
00273                     try:
00274                         fallback_func = self.fallback_mapping[methodname]
00275                         result = fallback_func(
00276                             self, conn, ident, obj, *args, **kwds
00277                             )
00278                         msg = ('#RETURN', result)
00279                     except Exception:
00280                         msg = ('#TRACEBACK', format_exc())
00281 
00282             except EOFError:
00283                 util.debug('got EOF -- exiting thread serving %r',
00284                            threading.current_thread().name)
00285                 sys.exit(0)
00286 
00287             except Exception:
00288                 msg = ('#TRACEBACK', format_exc())
00289 
00290             try:
00291                 try:
00292                     send(msg)
00293                 except Exception as e:
00294                     send(('#UNSERIALIZABLE', repr(msg)))
00295             except Exception as e:
00296                 util.info('exception in thread serving %r',
00297                         threading.current_thread().name)
00298                 util.info(' ... message was %r', msg)
00299                 util.info(' ... exception was %r', e)
00300                 conn.close()
00301                 sys.exit(1)

Here is the call graph for this function:

Here is the caller graph for this function:

Run the server forever

Definition at line 173 of file managers.py.

00173 
00174     def serve_forever(self):
00175         '''
00176         Run the server forever
00177         '''
00178         current_process()._manager_server = self
00179         try:
00180             try:
00181                 while 1:
00182                     try:
00183                         c = self.listener.accept()
00184                     except (OSError, IOError):
00185                         continue
00186                     t = threading.Thread(target=self.handle_request, args=(c,))
00187                     t.daemon = True
00188                     t.start()
00189             except (KeyboardInterrupt, SystemExit):
00190                 pass
00191         finally:
00192             self.stop = 999
00193             self.listener.close()

Here is the call graph for this function:

Shutdown this process

Definition at line 344 of file managers.py.

00344 
00345     def shutdown(self, c):
00346         '''
00347         Shutdown this process
00348         '''
00349         try:
00350             try:
00351                 util.debug('manager received shutdown message')
00352                 c.send(('#RETURN', None))
00353 
00354                 if sys.stdout != sys.__stdout__:
00355                     util.debug('resetting stdout, stderr')
00356                     sys.stdout = sys.__stdout__
00357                     sys.stderr = sys.__stderr__
00358 
00359                 util._run_finalizers(0)
00360 
00361                 for p in active_children():
00362                     util.debug('terminating a child process of manager')
00363                     p.terminate()
00364 
00365                 for p in active_children():
00366                     util.debug('terminating a child process of manager')
00367                     p.join()
00368 
00369                 util._run_finalizers()
00370                 util.info('manager exiting with exitcode 0')
00371             except:
00372                 import traceback
00373                 traceback.print_exc()
00374         finally:
00375             exit(0)

Here is the call graph for this function:


Member Data Documentation

Definition at line 166 of file managers.py.

Definition at line 161 of file managers.py.

Initial value:
{
        '__str__':fallback_str,
        '__repr__':fallback_repr,
        '#GETVALUE':fallback_getvalue
        }

Definition at line 311 of file managers.py.

Definition at line 168 of file managers.py.

Definition at line 169 of file managers.py.

Definition at line 165 of file managers.py.

Definition at line 170 of file managers.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Initial value:
['shutdown', 'create', 'accept_connection', 'get_methods',
              'debug_info', 'number_of_objects', 'dummy', 'incref', 'decref']

Definition at line 155 of file managers.py.

Definition at line 160 of file managers.py.

Definition at line 171 of file managers.py.


The documentation for this class was generated from the following file: