Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion proto/marshal/marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import abc
import enum
import threading

from google.protobuf import message
from google.protobuf import duration_pb2
Expand Down Expand Up @@ -256,6 +257,7 @@ class Marshal(BaseMarshal):
"""

_instances = {}
_instances_lock = threading.Lock()

def __new__(cls, *, name: str):
"""Create a marshal instance.
Expand All @@ -267,7 +269,13 @@ def __new__(cls, *, name: str):
"""
klass = cls._instances.get(name)
if klass is None:
klass = cls._instances[name] = super().__new__(cls)
with cls._instances_lock:
klass = cls._instances.get(name)
if klass is None:
klass = super().__new__(cls)
instances_copy = cls._instances.copy()
instances_copy[name] = klass
cls._instances = instances_copy

return klass

Expand Down
Loading