Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5570
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <eric.frederich@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'sys': 0.04; '2.7': 0.05; 'args': 0.07; 'python?': 0.07; 'python': 0.07; 'okay': 0.09; 'files.': 0.10; 'scripts': 0.10; 'written': 0.12; 'received:74.125.82.174': 0.12; 'received:mail- wy0-f174.google.com': 0.12; 'def': 0.13; 'fname': 0.16; 'run(self):': 0.16; 'with?': 0.16; 'versions': 0.18; 'this?': 0.18; 'skip:r 30': 0.19; 'seems': 0.21; 'code': 0.22; 'trying': 0.23; 'subject:code': 0.23; 'example': 0.24; 'version': 0.25; 'skip:# 10': 0.25; 'skip:l 30': 0.25; 'instead': 0.26; 'message- id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'class': 0.29; 'class.': 0.29; 'yet': 0.30; 'queue': 0.31; 'worker': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'things': 0.33; 'break': 0.33; 'lines': 0.34; 'using': 0.34; 'actually': 0.34; 'there': 0.35; 'print': 0.35; 'flag': 0.35; 'skip:@ 10': 0.35; 'try:': 0.35; 'doing': 0.36; 'some': 0.37; 'run': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'anything': 0.38; 'set': 0.39; 'skip:s 30': 0.39; 'to:addr:python.org': 0.39; 'except': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'best': 0.60; 'results': 0.61; 'below': 0.63 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=RLaSmla25hZH034+Tasj+prTv63DnBfa7Xj8YdqwQOM=; b=Gn1L6H31e5LEL6j4kh6fMVndF1d+1bFcUa2wB6yHE/AkoDEUoU3NkKXNJuCRmgvku1 tDaQbdoI79fNFvnHagOqwMzUT9CAXdemImPPjCwJ796dtke+7PYT1Uz1xRhAIN/JZ7xY ATEqfVryIQwNsglXI/WFgTxVgABgdDJ1FXO/o= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=f3BliAsy5N48qti3+ytktmHf/ZkRRqBjbgv9C0ng1EGDplJlbiXFVUbCANoshVcyqV CGesU5uARR7SCiNN+tCslByhMcrTLDQmolIzPyx0huG637lboVsrQzVWKvq+Q6f8VaOV xD789au6gss32meN13hdOpSaa89aT9k/7W07w= |
| MIME-Version | 1.0 |
| Date | Tue, 17 May 2011 11:14:21 -0400 |
| Subject | portable multiprocessing code |
| From | Eric Frederich <eric.frederich@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1674.1305645262.9059.python-list@python.org> (permalink) |
| Lines | 61 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1305645262 news.xs4all.nl 49184 [::ffff:82.94.164.166]:43631 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:5570 |
Show key headers only | View raw
I have written some code using Python 2.7 but I'd like these scripts
to be able to run on Red Hat 5's 2.4.3 version of Python which doesn't
have multiprocessing.
I can try to import multiprocessing and set a flag as to whether it is
available. Then I can create a Queue.Queue instead of a
multiprocessing.Queue for the arg_queue and result_queue.
Without actually trying this yet it seems like things would work okay
except for the Worker class. It seems I can conditionally replace
multiprocessing.Queue with Queue.Queue, but is there anything to
replace multiprocessing.Process with?
Are there any best practices for doing something like this?
Below is a dumb example that just counts lines in files.
What would be the best way to make this runnable in older (2.4.3)
versions of Python?
#!/usr/bin/env python
import sys
import os
import multiprocessing
import Queue
fnames = sys.argv[1:]
def SimpleWorker(func):
class SimpleWorker_wrapped(multiprocessing.Process):
def __init__(self, arg_queue, result_queue):
super(SimpleWorker_wrapped, self).__init__()
self.arg_queue = arg_queue
self.result_queue = result_queue
def run(self):
while True:
try:
args = self.arg_queue.get_nowait()
except Queue.Empty:
break
self.result_queue.put(func(*args))
return SimpleWorker_wrapped
@SimpleWorker
def line_counter(fname):
lc = len(open(fname).read().splitlines())
return fname, lc
arg_queue = multiprocessing.Queue()
result_queue = multiprocessing.Queue()
for fname in fnames:
arg_queue.put((fname,))
for i in range(multiprocessing.cpu_count()):
w = line_counter(arg_queue, result_queue)
w.start()
results = {}
for fname in sorted(fnames):
while fname not in results:
n, i = result_queue.get()
results[n] = i
print "%-40s %d" % (fname, results[fname])
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
portable multiprocessing code Eric Frederich <eric.frederich@gmail.com> - 2011-05-17 11:14 -0400
csiph-web