diff options
author | Jesse Noller <jnoller@gmail.com> | 2009-04-02 05:17:26 (GMT) |
---|---|---|
committer | Jesse Noller <jnoller@gmail.com> | 2009-04-02 05:17:26 (GMT) |
commit | 7152f6d9156a19a9f1b31e313d2c19d840dc089d (patch) | |
tree | 69d6b77c1daaa07f9c7a1aa950ed65ae713f504c /Lib/multiprocessing | |
parent | d56bab47f1c8432f4d7d3b92d0b8f8320f369fb7 (diff) | |
download | cpython-7152f6d9156a19a9f1b31e313d2c19d840dc089d.zip cpython-7152f6d9156a19a9f1b31e313d2c19d840dc089d.tar.gz cpython-7152f6d9156a19a9f1b31e313d2c19d840dc089d.tar.bz2 |
Add custom initializer argument to multiprocess.Manager*, courtesy of lekma
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r-- | Lib/multiprocessing/managers.py | 13 | ||||
-rw-r--r-- | Lib/multiprocessing/pool.py | 3 |
2 files changed, 13 insertions, 3 deletions
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index 02e96b9..fde0e40 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -475,12 +475,15 @@ class BaseManager(object): dispatch(conn, None, 'dummy') self._state.value = State.STARTED - def start(self): + def start(self, initializer=None, initargs=()): ''' Spawn a server process for this manager object ''' assert self._state.value == State.INITIAL + if initializer is not None and not hasattr(initializer, '__call__'): + raise TypeError('initializer must be a callable') + # pipe over which we will retrieve address of server reader, writer = connection.Pipe(duplex=False) @@ -488,7 +491,7 @@ class BaseManager(object): self._process = Process( target=type(self)._run_server, args=(self._registry, self._address, self._authkey, - self._serializer, writer), + self._serializer, writer, initializer, initargs), ) ident = ':'.join(str(i) for i in self._process._identity) self._process.name = type(self).__name__ + '-' + ident @@ -509,10 +512,14 @@ class BaseManager(object): ) @classmethod - def _run_server(cls, registry, address, authkey, serializer, writer): + def _run_server(cls, registry, address, authkey, serializer, writer, + initializer=None, initargs=()): ''' Create a server, report its address and run it ''' + if initializer is not None: + initializer(*initargs) + # create server server = cls._Server(registry, address, authkey, serializer) diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index 9da27d4..bc7e8f1 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -92,6 +92,9 @@ class Pool(object): except NotImplementedError: processes = 1 + if initializer is not None and not hasattr(initializer, '__call__'): + raise TypeError('initializer must be a callable') + self._pool = [] for i in range(processes): w = self.Process( |