Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'plenty': 0.04; 'python.': 0.05; 'arguments': 0.05; 'instance,': 0.05; 'memory.': 0.05; 'args': 0.07; 'familiarity': 0.07; 'read-only': 0.07; 'wrapper': 0.07; 'python': 0.07; 'default.': 0.09; 'investigate': 0.09; 'object.': 0.09; 'referenced': 0.09; 'subprocess': 0.09; 'to:name:python list': 0.09; 'pm,': 0.11; 'wrote:': 0.14; '(shared': 0.16; 'approaches.': 0.16; 'caches': 0.16; 'kitchen': 0.16; 'mmap': 0.16; 'neural': 0.16; 'non- trivial': 0.16; 'received:mindspring.com': 0.16; 'stay,': 0.16; 'subject:memory': 0.16; 'subject:vs.': 0.16; 'versa.': 0.16; 'input': 0.18; 'bytes': 0.19; 'object,': 0.19; 'temporary': 0.19; 'modules': 0.20; '(which': 0.21; 'not.': 0.22; 'header:In-Reply- To:1': 0.22; 'convert': 0.22; '(and': 0.22; "they've": 0.23; 'objects': 0.24; 'thus': 0.24; 'appears': 0.24; 'memory': 0.24; 'wrote': 0.25; 'classes': 0.26; 'object': 0.27; 'pass': 0.27; 'function': 0.27; 'lists': 0.28; 'shared': 0.29; 'depends': 0.29; 'implement': 0.30; 'get.': 0.31; 'lock': 0.31; 'objects.': 0.31; "one's": 0.31; 'it.': 0.31; 'to:addr:python-list': 0.32; 'another': 0.32; "i've": 0.33; 'answers': 0.33; 'module': 0.33; 'reference': 0.34; 'using': 0.34; 'actually': 0.34; 'difficult': 0.35; 'put': 0.35; 'yet,': 0.35; 'quite': 0.36; 'too': 0.36; 'rather': 0.36; 'familiar': 0.36; 'think': 0.36; 'charset:us- ascii': 0.36; 'data': 0.37; 'some': 0.37; 'raw': 0.37; 'should': 0.37; 'apr': 0.38; 'playing': 0.38; 'but': 0.38; 'pretty': 0.38; 'so,': 0.38; 'used': 0.38; 'unless': 0.38; 'room': 0.39; 'progress': 0.39; 'to:addr:python.org': 0.39; 'header:Mime- Version:1': 0.39; 'how': 0.39; 'received:24': 0.39; "it's": 0.40; 'might': 0.40; 'philip': 0.60; 'waiting': 0.61; 'here:': 0.61; 'back': 0.61; 'give': 0.61; 'header:Message-Id:1': 0.62; 'and,': 0.63; 'lower': 0.63; 'spend': 0.63; 'here': 0.65; 'subject:. ': 0.66; 'evaluate': 0.72; 'appliances,': 0.84; 'crossed': 0.84; 'john,': 0.84; 'questions:': 0.84; 'quo.': 0.84; 'received:69.73': 0.84; 'presumably': 0.91 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1082) Subject: Re: Multiprocessing, shared memory vs. pickled copies From: Philip Semanchuk In-Reply-To: <6ace38dc-33c6-44ab-a17a-084d62d666cb@w9g2000prg.googlegroups.com> Date: Mon, 4 Apr 2011 19:34:29 -0400 Content-Transfer-Encoding: quoted-printable References: <6ace38dc-33c6-44ab-a17a-084d62d666cb@w9g2000prg.googlegroups.com> To: Python list X-Mailer: Apple Mail (2.1082) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - deimos.nocdirect.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - semanchuk.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1301960081 news.xs4all.nl 41117 [::ffff:82.94.164.166]:37442 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2615 On Apr 4, 2011, at 4:20 PM, John Ladasky wrote: > I have been playing with multiprocessing for a while now, and I have > some familiarity with Pool. Apparently, arguments passed to a Pool > subprocess must be able to be pickled. =20 Hi John, multiprocessing's use of pickle is not limited to Pool. For instance, = objects put into a multiprocessing.Queue are also pickled, as are the = args to a multiprocessing.Process. So if you're going to use = multiprocessing, you're going to use pickle, and you need pickleable = objects.=20 > Pickling is still a pretty > vague progress to me, but I can see that you have to write custom > __reduce__ and __setstate__ methods for your objects. Well, that's only if one's objects don't support pickle by default. A = lot of classes do without any need for custom __reduce__ and = __setstate__ methods. Since you're apparently not too familiar with = pickle, I don't want you to get the false impression that it's a lot of = trouble. I've used pickle a number of times and never had to write = custom methods for it. > Now, I don't know that I actually HAVE to pass my neural network and > input data as copies -- they're both READ-ONLY objects for the > duration of an evaluate function (which can go on for quite a while). > So, I have also started to investigate shared-memory approaches. I > don't know how a shared-memory object is referenced by a subprocess > yet, but presumably you pass a reference to the object, rather than > the whole object. Also, it appears that subprocesses also acquire a > temporary lock over a shared memory object, and thus one process may > well spend time waiting for another (individual CPU caches may > sidestep this problem?) Anyway, an implementation of a shared-memory > ndarray is here: There's no standard shared memory implementation for Python. The mmap = module is as close as you get. I wrote & support the posix_ipc and = sysv_ipc modules which give you IPC primitives (shared memory and = semaphores) in Python. They work well (IMHO) but they're *nix-only and = much lower level than multiprocessing. If multiprocessing is like a = kitchen well stocked with appliances, posix_ipc (and sysc_ipc) is like a = box of sharp knives. Note that mmap and my IPC modules don't expose Python objects. They = expose raw bytes in memory. YOu're still going to have to jump through = some hoops (...like pickle) to turn your Python objects into a = bytestream and vice versa. What might be easier than fooling around with boxes of sharp knives is = to convert your ndarray objects to Python lists. Lists are = pickle-friendly and easy to turn back into ndarray objects once they've = crossed the pickle boundary.=20 > When should one pickle and copy? When to implement an object in > shared memory? Why is pickling apparently such a non-trivial process > anyway? And, given that multi-core CPU's are apparently here to stay, > should it be so difficult to make use of them? My answers to these questions: 1) Depends 2) In Python, almost never unless you're using a nice wrapper like = shmarray.py 3) I don't think it's non-trivial =3D) 4) No, definitely not. Python will only get better at working with = multiple cores/CPUs, but there's plenty of room for improvement on the = status quo. Hope this helps Philip