8000 Fix multi process collision by initializing pid variable in master pr… · inxming/client_python@d858f0d · GitHub
[go: up one dir, main page]

Skip to content

Commit d858f0d

Browse files
aimestereobrian-brazil
authored andcommitted
Fix multi process collision by initializing pid variable in master process (prometheus#192)
1 parent 1cca4ed commit d858f0d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

prometheus_client/core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ def close(self):
395395
def _MultiProcessValue(_pidFunc=os.getpid):
396396
files = {}
397397
values = []
398+
pid = {'value': _pidFunc()}
398399
# Use a single global lock when in multi-processing mode
399400
# as we presume this means there is no threading going on.
400401
# This avoids the need to also have mutexes in __MmapDict.
@@ -413,22 +414,23 @@ def __init__(self, typ, metric_name, name, labelnames, labelvalues, multiprocess
413414

414415

415416
def __reset(self):
416-
self._pid = _pidFunc()
417417
typ, metric_name, name, labelnames, labelvalues, multiprocess_mode = self._params
418418
if typ == 'gauge':
419419
file_prefix = typ + '_' + multiprocess_mode
420420
else:
421421
file_prefix = typ
422422
if file_prefix not in files:
423423
filename = os.path.join(
424-
os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, self._pid))
424+
os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, pid['value']))
425425
files[file_prefix] = _MmapedDict(filename)
426426
self._file = files[file_prefix]
427427
self._key = json.dumps((metric_name, name, labelnames, labelvalues))
428428
self._value = self._file.read_value(self._key)
429429

430430
def __check_for_pid_change(self):
431-
if self._pid != _pidFunc():
431+
actual_pid = _pidFunc()
432+
if pid['value'] != actual_pid:
433+
pid['value'] = actual_pid
432434
# There has been a fork(), reset all the values.
433435
for f in files.values():
434436
f.close()

0 commit comments

Comments
 (0)
0