• Uncategorized

About python : Get-processor-id-in-Linux-Python-duplicate

Question Detail

I’m trying to deploy the project from VSCode to Azure function app that has Linux operating system.
I run the project locally on Windows using processor id by using following command:

os.popen("wmic cpu get processorid").read().replace("\n", "").replace("  ", "").replace(" ", "")[11:]

I need the analogue of the aforementioned command to get processor id for Linux system.
I tried below command on Ubuntu terminal and it worked:

sudo dmidecode --type processor

I get errors when I write it in VSCode and deploy it to Azure function app using following code (from [here]):

import platform, subprocess, logging

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "sudo dmidecode --type processor"
        logging.info("My command: %s.", str(subprocess.check_output(command, shell=True).strip()))
    return ""
    
get_processor_info()

Output of the error is: ”
Result: Failure Exception: CalledProcessError: Command ‘[‘sudo’, ‘dmidecode’, ‘–type’, ‘processor’]’ returned non-zero exit status 1. Stack: File “/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py”, line 405, in _handle__invocation_request invocation_id, fi_context, fi.func, args) File “/usr/local/lib/python3.7/concurrent/futures/thread.py”, line 57, in run result = self.fn(*self.args, **self.kwargs) File “/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py”, line 612, in _run_sync_func func)(params) File “/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/extension.py”, line 215, in _raw_invocation_wrapper result = function(**args) File “/home/site/wwwroot/timer_trig_05/init.py”, line 79, in main logging.info(“My command: %s.”, str(get_processor_info())) File “/home/site/wwwroot/timer_trig_05/init.py”, line 76, in get_processor_info return subprocess.check_output(cmd_lst) File “/usr/local/lib/python3.7/subprocess.py”, line 411, in check_output **kwargs).stdout File “/usr/local/lib/python3.7/subprocess.py”, line 512, in run output=stdout, stderr=stderr).”

What am I doing wrong? Or maybe there’re any other ways to extract the processor id?

Question Answer

I guess it’s a typo but the command you pass to the subprocess.checkoutput is sudo vim and not sudo dmidecode --type processor.
Besides, just like in the Darwin case, arguments should be passed as a list of string.
I changed the return value to match the other cases:

import platform, subprocess, logging

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "sudo dmidecode --type processor"
        cmd_lst = command.split(' ')
        return subprocess.check_output(cmd_lst)
    return ""
    
print(get_processor_info())

Edit: like @CharlesDuffy rightfully pointed out, you can match the other cases by writing it this way:

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        return subprocess.check_output(['sudo', 'dmidecode', '--type', 'processor'])
    return ""

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.