File: utils.py

package info (click to toggle)
python-sqt 0.8.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 824 kB
  • sloc: python: 5,964; sh: 38; makefile: 10
file content (27 lines) | stat: -rw-r--r-- 606 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import re

def available_cpu_count():
	"""
	Number of available virtual or physical CPUs on this system.

	Adapted from http://stackoverflow.com/a/1006301/715090
	"""
	# cpuset may restrict the number of available processors
	cpus = 1
	try:
		m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$',
						open('/proc/self/status').read())
		if m:
			res = bin(int(m.group(1).replace(',', ''), 16)).count('1')
			if res > 0:
				cpus = res
	except IOError:
		pass

	try:
		import multiprocessing
		cpus = min(cpus, multiprocessing.cpu_count())
	except (ImportError, NotImplementedError):
		pass
	return cpus