import os
import re
import subprocess
 
 
def available_cpu_count():
    """ Number of available virtual or physical CPUs on this system, i.e.
    user/real as output by time(1) when called with an optimally scaling
    userspace-only program"""
 
 
    # Python 2.6+
    try:
        import multiprocessing
        return multiprocessing.cpu_count()
    except (ImportError, NotImplementedError):
        pass
 
    # http://code.google.com/p/psutil/
    try:
        import psutil
        return psutil.NUM_CPUS
    except (ImportError, AttributeError):
        pass
 
    # Windows
    try:
        res = int(os.environ['NUMBER_OF_PROCESSORS'])
 
        if res > 0:
            return res
    except (KeyError, ValueError):
        pass
 
    raise Exception('Can not determine number of CPUs on this system')