Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68902

advice on sub-classing multiprocessing.Process and multiprocessing.BaseManager

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <matt.newville@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.020
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'cache': 0.07; 'variables': 0.07; 'advance': 0.07; 'advice.': 0.09; 'here?': 0.09; 'python': 0.11; 'def': 0.12; '--matt': 0.16; 'caching': 0.16; 'fine.': 0.16; 'overwriting': 0.16; 'subclass': 0.16; 'subclassing': 0.16; 'subject:sub': 0.16; 'threads,': 0.16; 'sender:addr:gmail.com': 0.17; 'code.': 0.18; 'library': 0.18; 'variable': 0.18; 'addition,': 0.20; 'seems': 0.21; 'looks': 0.24; 'sort': 0.25; 'idea': 0.28; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'doc': 0.31; 'anyone': 0.31; 'class': 0.32; 'there.': 0.32; 'maintaining': 0.32; 'skip:m 30': 0.32; 'interface': 0.32; 'run': 0.32; 'open': 0.33; 'trouble': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; 'advice': 0.35; 'connection': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'interface,': 0.36; 'thanks': 0.36; 'similar': 0.36; 'too': 0.37; 'clear': 0.37; 'connections': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'little': 0.38; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'easy': 0.60; 'new': 0.61; 'simple': 0.61; 'making': 0.63; 'such': 0.63; 'skip:n 10': 0.64; 'provide': 0.64; 'relatively': 0.65; 'system)': 0.69; 'easier,': 0.84; 'here!': 0.84; 'light-weight': 0.84; 'trouble.': 0.91; 'state.': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:from:date:message-id:subject:to:content-type; bh=0qmV+268fdSajeUx24s1w9GF1pC1prRC50uXysJy+mc=; b=hcdAUYEe6j4SaRqgzcyQUE/H+0yisqKr44S/o53Fv4hyPR9aBVk7q4trood2cSQ2Mt yx9ggvoUO6ZX2q8VcTrli4kZ/nXPUmV+5vmQGvcKYGwB8AHhEvWIJyqao6+JC02DZXvH czVc/5uTuag7B8BS4d87C2ku8C9eN28ZwA0EknSoQj8rzsP4tBVFgkg9hSiHGpEKsshx RIU5fLjmKptVlNgQscDW3/CgRFHzJl37h9uhBjpJfztcv6ig+oIFrItGpTALawzuhJHd LznCER9/qG33PIrIoDuf+Yy4XqvtZ9cJeXjANa3REh91AJZ/PRrcSZe9aNSvW+bPCBUU lmcg==
X-Received by 10.50.122.104 with SMTP id lr8mr3928818igb.4.1395692730359; Mon, 24 Mar 2014 13:25:30 -0700 (PDT)
MIME-Version 1.0
Sender matt.newville@gmail.com
From Matt Newville <newville@cars.uchicago.edu>
Date Mon, 24 Mar 2014 15:24:34 -0500
X-Google-Sender-Auth BlinPe6e490ELkzodhI2Atmv92M
Subject advice on sub-classing multiprocessing.Process and multiprocessing.BaseManager
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8465.1395692738.18130.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395692738 news.xs4all.nl 2950 [2001:888:2000:d::a6]:47493
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68902

Show key headers only | View raw


I'm maintaining a python interface to a C library for a distributed
control system (EPICS, sort of a SCADA system) that does a large
amount of relatively light-weight network I/O.   In order to keep many
connections open and responsive, and to provide a simple interface,
the python library keeps a global store of connection state.

This works well for single processes and threads, but not so well for
multiprocessing, where the global state causes trouble.  The issue is
not too difficult to work around (ie, completely clear any such global
cache and insist that new connections be established in each process),
but easy to forget.

To make this easier, I have a function to clear the global cache,

def clear_my_cache():
     # empty global variables caching network connections
     return

and then subclass of multiprocessing.Process like:

class MyProcess(multiprocessing.Process):
    def __init__(self, **kws):
        multiprocessing.Process.__init__(self, **kws)

    def run(self):
        clear_my_cache()
        mp.Process.run(self)

This works fine.  I can subclass multiprocessing.pool.Pool too, as it
uses Process as a class variable (removing doc strings):

class Pool(object):
    Process = Process

    def __init__(self, processes=None, initializer=None, initargs=(),
                 maxtasksperchild=None):

and then uses self.Process in its Pool._repopulate_pool().  That makes
subclassing Pool is as easy as (removing doc strings):

class MyPool(multiprocssing.pool.Pool):
    def __init__(self, **kws):
        self.Process = MyProcess
        mp_pool.Pool.__init__(self, **kws)

I'm very pleased to need so little code here!  But, I run into trouble
when I try to subclass any of the Managers().  It looks like I would
have to make a nearly-identical copy of ~30 lines of
BaseManager.start() as it calls multiprocessing.Process() to create
processes there.   In addition, it looks like subclassing
multiprocessing.managers.SyncManager would mean making a
near-identical copy of a similar amount of code.

I'd be willing to do this, but it seems like a bad idea  -- I much
prefer overwriting self.Process as for Pool.

Does anyone have any advice for the best approach here?  Should, like
Pool,  BaseManager also use a class variable (Process = Process)?

Thanks in advance for any advice.

--Matt

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

advice on sub-classing multiprocessing.Process and multiprocessing.BaseManager Matt Newville <newville@cars.uchicago.edu> - 2014-03-24 15:24 -0500

csiph-web