# HG changeset patch # User paulb # Date 1209861500 0 # Node ID 8d875112948e9ae4977b95c8549c1d27b363cfa5 # Parent ae8accb893a4c6290db27c9f4e9c54979aa71cbd [project @ 2008-05-04 00:38:20 by paulb] Added a new function: get_number_of_cores. Updated release information. diff -r ae8accb893a4 -r 8d875112948e PKG-INFO --- a/PKG-INFO Thu Jun 19 21:45:03 2008 +0200 +++ b/PKG-INFO Sun May 04 00:38:20 2008 +0000 @@ -1,12 +1,12 @@ Metadata-Version: 1.1 Name: pprocess -Version: 0.3.1 +Version: 0.3.2 Author: Paul Boddie Author-email: paul at boddie org uk Maintainer: Paul Boddie Maintainer-email: paul at boddie org uk Home-page: http://www.boddie.org.uk/python/pprocess.html -Download-url: http://www.boddie.org.uk/python/downloads/pprocess-0.3.1.tar.gz +Download-url: http://www.boddie.org.uk/python/downloads/pprocess-0.3.2.tar.gz Summary: Elementary parallel programming for Python License: LGPL (version 3 or later) Description: The pprocess module provides elementary support for parallel diff -r ae8accb893a4 -r 8d875112948e README.txt --- a/README.txt Thu Jun 19 21:45:03 2008 +0200 +++ b/README.txt Sun May 04 00:38:20 2008 +0000 @@ -112,6 +112,12 @@ This software depends on standard library features which are stated as being available only on "UNIX"; it has only been tested on a GNU/Linux system. +New in pprocess 0.3.2 (Changes since pprocess 0.3.1) +---------------------------------------------------- + + * Added a utility function to detect and return the number of processor + cores available. + New in pprocess 0.3.1 (Changes since pprocess 0.3) -------------------------------------------------- diff -r ae8accb893a4 -r 8d875112948e pprocess.py --- a/pprocess.py Thu Jun 19 21:45:03 2008 +0200 +++ b/pprocess.py Sun May 04 00:38:20 2008 +0000 @@ -4,7 +4,7 @@ A simple parallel processing API for Python, inspired somewhat by the thread module, slightly less by pypar, and slightly less still by pypvm. -Copyright (C) 2005, 2006, 2007 Paul Boddie +Copyright (C) 2005, 2006, 2007, 2008 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free @@ -20,7 +20,7 @@ with this program. If not, see . """ -__version__ = "0.3.1" +__version__ = "0.3.2" import os import sys @@ -32,6 +32,11 @@ except ImportError: import pickle +try: + set +except NameError: + from sets import Set as set + # Communications. class AcknowledgementError(Exception): @@ -523,6 +528,43 @@ # Utility functions. +_cpuinfo_fields = "physical id", "core id" + +def get_number_of_cores(): + + """ + Return the number of distinct, genuine processor cores. If the platform is + not supported by this function, None is returned. + """ + + try: + f = open("/proc/cpuinfo") + try: + processors = set() + processor = [None, None] + + for line in f.xreadlines(): + for i, field in enumerate(_cpuinfo_fields): + if line.startswith(field): + t = line.split(":") + processor[i] = int(t[1].strip()) + break + else: + if line.startswith("processor") and processor[0] is not None: + processors.add(tuple(processor)) + processor = [None, None] + + if processor[0] is not None: + processors.add(tuple(processor)) + + return len(processors) + + finally: + f.close() + + except OSError: + return None + def create(): """