How can I check nbr of cores of computer?

Daniel da Silva ddasilva at umd.edu
Wed Jul 30 19:36:21 EDT 2008


Single line using /proc/cpuinfo:

numprocs = [ int(line.strip()[-1]) for line in open('/proc/cpuinfo', 'r') if \
              line.startswith('processor') ][-1] + 1


On Wed, Jul 30, 2008 at 2:16 PM, Dan Upton <upton at virginia.edu> wrote:
>
> On Wed, Jul 30, 2008 at 2:22 PM, John Nagle <nagle at animats.com> wrote:
> > defn noob wrote:
> >>
> >> How can I check how many cores my computer has?
> >> Is it possible to do this in a Python-app?
> >
> >    Why do you care?  Python can't use more than one of them at
> > a time anyway.
>
> Per Python process, but you might fork multiple processes and want to
> know how many cores there are to know how many to fork, and which
> cores to pin them to.  (I don't know if there's a direct way in Python
> to force it to a certain core, so I instead just wrote an extension to
> interface with sched_setaffinity on Linux.)
>
> On Linux, an almost assuredly non-ideal way to find out the number of
> cores is to read /proc/cpuinfo and look for the highest-numbered
> "processor: " line (and add 1).
> --
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list