| 1 | from subprocess import Popen, PIPE |
| 2 | from plyer.facades import Processors |
| 3 | from plyer.utils import whereis_exe |
| 4 | |
| 5 | from os import environ |
| 6 | |
| 7 | |
| 8 | class LinuxProcessors(Processors): |
| 9 | def _get_state(self): |
| 10 | old_lang = environ.get('LANG') |
| 11 | environ['LANG'] = 'C' |
| 12 | |
| 13 | status = {"Number_of_Processors": None} |
| 14 | |
| 15 | dev = "--all" |
| 16 | nproc_process = Popen( |
| 17 | ["nproc", dev], |
| 18 | stdout=PIPE |
| 19 | ) |
| 20 | output = nproc_process.communicate()[0] |
| 21 | |
| 22 | environ['LANG'] = old_lang |
| 23 | |
| 24 | if not output: |
| 25 | return status |
| 26 | |
| 27 | status['Number_of_Processors'] = output.rstrip() |
| 28 | |
| 29 | return status |
| 30 | |
| 31 | |
| 32 | def instance(): |
| 33 | import sys |
| 34 | if whereis_exe('nproc'): |
| 35 | return LinuxProcessors() |
| 36 | sys.stderr.write("nproc not found.") |
| 37 | return Processors() |