How can I check nbr of cores of computer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • defn noob

    How can I check nbr of cores of computer?

    How can I check how many cores my computer has?
    Is it possible to do this in a Python-app?
  • gbs

    #2
    Re: How can I check nbr of cores of computer?

    On Tue, 29 Jul 2008 15:53:47 -0700, defn noob wrote:
    How can I check how many cores my computer has? Is it possible to do
    this in a Python-app?
    Well you can try the functions in the 'platform' module, although in my
    box(debian) nothing useful comes out. I don't think there's a simple &
    portable way, you probably have to find one specific to your platform .
    (if you are on linux, you can parse the output of 'cat /proc/cpuinfo')

    Comment

    • Mensanator

      #3
      Re: How can I check nbr of cores of computer?

      On Jul 29, 5:53 pm, defn noob <circularf...@y ahoo.sewrote:
      How can I check how many cores my computer has?
      Is it possible to do this in a Python-app?
      If you're using Windows, get PyWin32:

      win32api.GetSys temInfo
      tuple = GetSystemInfo()

      Retrieves information about the current system.

      Win32 API References

      Search for GetSystemInfo at msdn, google or google groups.

      Return Value
      The return value is a tuple of 9 values,
      which corresponds to the Win32 SYSTEM_INFO structure.
      The element names are:
      dwOemId
      dwPageSize
      lpMinimumApplic ationAddress
      lpMaximumApplic ationAddress
      dwActiveProcess orMask
      dwNumberOfProce ssors
      dwProcessorType
      dwAllocationGra nularity
      (wProcessorLeve l,wProcessorRev ision)

      >>import win32api
      >>win32api.GetS ystemInfo()
      (0, 4096, 65536, 2147418111, 3L, 2, 586, 65536, (6, 3846))
      |
      processors

      Comment

      • alex.gaynor@gmail.com

        #4
        Re: How can I check nbr of cores of computer?

        On Jul 29, 7:44 pm, Mensanator <mensana...@aol .comwrote:
        On Jul 29, 5:53 pm, defn noob <circularf...@y ahoo.sewrote:
        >
        How can I check how many cores my computer has?
        Is it possible to do this in a Python-app?
        >
        If you're using Windows, get PyWin32:
        >
        win32api.GetSys temInfo
        tuple = GetSystemInfo()
        >
        Retrieves information about the current system.
        >
        Win32 API References
        >
        Search for GetSystemInfo at msdn, google or google groups.
        >
        Return Value
        The return value is a tuple of 9 values,
        which corresponds to the Win32 SYSTEM_INFO structure.
        The element names are:
        dwOemId
        dwPageSize
        lpMinimumApplic ationAddress
        lpMaximumApplic ationAddress
        dwActiveProcess orMask
        dwNumberOfProce ssors
        dwProcessorType
        dwAllocationGra nularity
        (wProcessorLeve l,wProcessorRev ision)
        >
        >import win32api
        >win32api.GetSy stemInfo()
        >
        (0, 4096, 65536, 2147418111, 3L, 2, 586, 65536, (6, 3846))
                                         |
                                         processors
        This: http://pyprocessing.berlios.de/ has a method that returns CPU
        count.

        Comment

        • John Nagle

          #5
          Re: How can I check nbr of cores of computer?

          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.

          John Nagle

          Comment

          • Dan Upton

            #6
            Re: How can I check nbr of cores of computer?

            On Wed, Jul 30, 2008 at 2:22 PM, John Nagle <nagle@animats. comwrote:
            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_setaffini ty 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).

            Comment

            • Leonhard Vogt

              #7
              Re: How can I check nbr of cores of computer?

              defn noob schrieb:
              How can I check how many cores my computer has?
              Is it possible to do this in a Python-app?
              processing (http://pyprocessing.berlios.de/)
              has an utility function processing.cpuC ount().

              Leo

              Comment

              • Daniel da Silva

                #8
                Re: How can I check nbr of cores of computer?

                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@virginia .eduwrote:
                >
                On Wed, Jul 30, 2008 at 2:22 PM, John Nagle <nagle@animats. comwrote:
                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_setaffini ty 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

                Comment

                • Steven D'Aprano

                  #9
                  Re: How can I check nbr of cores of computer?

                  On Wed, 30 Jul 2008 11:22:12 -0700, John Nagle 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.
                  Why do you care why he cares? And he didn't ask how to *use* multiple
                  cores, but how to detect if they're there.

                  Maybe all he wants is to write a toy program that outputs "Hello Fred,
                  your computer has N cores in the CPU. Have a nice day!".

                  I don't expect that Python will have a built-in core-counting function.
                  You would probably need to ask the operating system. On Linux, you could
                  do this:

                  import os
                  text = os.popen('cat /proc/cpuinfo').read( )


                  How you would do it in Windows or Mac, I have no idea.



                  --
                  Steven

                  Comment

                  • Duncan Booth

                    #10
                    Re: How can I check nbr of cores of computer?

                    Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrote:
                    I don't expect that Python will have a built-in core-counting function.
                    You would probably need to ask the operating system. On Linux, you could
                    do this:
                    >
                    import os
                    text = os.popen('cat /proc/cpuinfo').read( )
                    >
                    >
                    How you would do it in Windows or Mac, I have no idea.
                    >
                    One way on windows:
                    >>import win32pdhutil
                    >>def howmanycores():
                    for i in range(9999):
                    try: win32pdhutil.Ge tPerformanceAtt ributes("Proces sor(%d)" % i,"%
                    Processor Time")
                    except: break
                    return i
                    >>print howmanycores()
                    2

                    --
                    Duncan Booth http://kupuguy.blogspot.com

                    Comment

                    Working...