Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve forking support #804

Merged
merged 21 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix formatting
  • Loading branch information
vickenty committed Dec 14, 2023
commit ec8f50455c528d5be9a5075941f33ddb0b688ac5
12 changes: 8 additions & 4 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
SUPPORTS_FORKING = hasattr(os, "register_at_fork") and not os.environ.get("DD_DOGSTATSD_DISABLE_FORK_SUPPORT", None)
TRACK_INSTANCES = not os.environ.get("DD_DOGSTATSD_DISABLE_INSTANCE_TRACKING", None)

_instances = weakref.WeakSet() # type: weakref.WeakSet
_instances = weakref.WeakSet() # type: weakref.WeakSet


def pre_fork():
"""Prepare all client instances for a process fork.
Expand All @@ -109,6 +110,7 @@ def pre_fork():
for c in _instances:
c.pre_fork()


def post_fork():
"""Restore all client instances after a fork.

Expand All @@ -117,15 +119,16 @@ def post_fork():
for c in _instances:
c.post_fork()


if SUPPORTS_FORKING:
os.register_at_fork(before=pre_fork, after_in_child=post_fork, after_in_parent=post_fork) # type: ignore
os.register_at_fork(before=pre_fork, after_in_child=post_fork, after_in_parent=post_fork) # type: ignore


# pylint: disable=useless-object-inheritance,too-many-instance-attributes
# pylint: disable=too-many-arguments,too-many-locals
class DogStatsd(object):
OK, WARNING, CRITICAL, UNKNOWN = (0, 1, 2, 3)


def __init__(
self,
host=DEFAULT_HOST, # type: Text
Expand Down Expand Up @@ -312,7 +315,8 @@ def __init__(
Default: 0 (no wait)
:type sender_queue_timeout: float

:param track_instance: Keep track of this instance and automatically handle cleanup when os.fork() is called, if supported.
:param track_instance: Keep track of this instance and automatically handle cleanup when os.fork() is called,
if supported.
Default: True.
:type track_instance: boolean
"""
Expand Down
Loading