1232
1232
DEFAULT_MULTIPLIER = 2
1235
def _calculate_workers():
1237
Determine the number of worker processes based on the CPU
1238
count of the unit containing the application.
1240
Workers will be limited to MAX_DEFAULT_WORKERS in
1241
container environments where no worker-multipler configuration
1244
@returns int: number of worker processes to use
1246
multiplier = config('worker-multiplier') or DEFAULT_MULTIPLIER
1247
count = int(_num_cpus() * multiplier)
1248
if multiplier > 0 and count == 0:
1251
if config('worker-multiplier') is None and is_container():
1252
# NOTE(jamespage): Limit unconfigured worker-multiplier
1253
# to MAX_DEFAULT_WORKERS to avoid insane
1254
# worker configuration in LXD containers
1256
# Reference: https://pad.lv/1665270
1257
count = min(count, MAX_DEFAULT_WORKERS)
1264
Compatibility wrapper for calculating the number of CPU's
1267
@returns: int: number of CPU cores detected
1270
return psutil.cpu_count()
1271
except AttributeError:
1272
return psutil.NUM_CPUS
1235
1275
class WorkerConfigContext(OSContextGenerator):
1239
# NOTE: use cpu_count if present (16.04 support)
1240
if hasattr(psutil, 'cpu_count'):
1241
return psutil.cpu_count()
1243
return psutil.NUM_CPUS
1245
1277
def __call__(self):
1246
multiplier = config('worker-multiplier') or DEFAULT_MULTIPLIER
1247
count = int(self.num_cpus * multiplier)
1248
if multiplier > 0 and count == 0:
1251
if config('worker-multiplier') is None and is_container():
1252
# NOTE(jamespage): Limit unconfigured worker-multiplier
1253
# to MAX_DEFAULT_WORKERS to avoid insane
1254
# worker configuration in LXD containers
1256
# Reference: https://pad.lv/1665270
1257
count = min(count, MAX_DEFAULT_WORKERS)
1259
ctxt = {"workers": count}
1278
ctxt = {"workers": _calculate_workers()}
1265
1284
def __init__(self, name=None, script=None, admin_script=None,
1266
1285
public_script=None, process_weight=1.00,
1267
admin_process_weight=0.75, public_process_weight=0.25):
1286
admin_process_weight=0.25, public_process_weight=0.75):
1268
1287
self.service_name = name
1269
1288
self.user = name
1270
1289
self.group = name
1276
1295
self.public_process_weight = public_process_weight
1278
1297
def __call__(self):
1279
multiplier = config('worker-multiplier') or 1
1280
total_processes = self.num_cpus * multiplier
1298
total_processes = _calculate_workers()
1282
1300
"service_name": self.service_name,
1283
1301
"user": self.user,