Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit d4aef2e

Browse files
ThatRfernandgildea
authored andcommitted
Use the singleton pattern in logger to only create one object.
----Release Notes---- [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=117149398
1 parent 493afc5 commit d4aef2e

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

google/cloud/dataflow/worker/logger.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@
2828

2929
class PerThreadLoggingContext(object):
3030
"""A context manager to add per thread attributes."""
31+
_instance = dict()
32+
33+
def __new__(cls, *args, **kwargs):
34+
# TODO(robertwb): make the class generic, this is special-cased to save
35+
# time on the DoFn
36+
if not args and len(kwargs) == 1 and 'step_name' in kwargs:
37+
k = kwargs['step_name']
38+
if k not in cls._instance:
39+
cls._instance[k] = super(PerThreadLoggingContext, cls).__new__(
40+
cls, *args, **kwargs)
41+
return cls._instance[k]
42+
else:
43+
return super(PerThreadLoggingContext, cls).__new__(cls, *args, **kwargs)
3144

3245
def __init__(self, *args, **kwargs):
3346
if args:

0 commit comments

Comments
 (0)