@@ -415,6 +415,37 @@ def __init__(
415415
416416 self ._queue = None
417417 if not disable_background_sender :
418+ self .enable_background_sender (sender_queue_size , sender_queue_timeout )
419+
420+ def enable_background_sender (self , sender_queue_size = 0 , sender_queue_timeout = 0 ):
421+ """
422+ Use a background thread to communicate with the dogstatsd server.
423+ When enabled, a background thread will be used to send metric payloads to the Agent.
424+
425+ Applications should call wait_for_pending() before exiting to make sure all pending payloads are sent.
426+
427+ This method is not thread safe and should not be called concurrently with other methods on the current object.
428+ Normally, this should be called shortly after process initialization (for example from a post-fork hook in a
429+ forking server).
430+
431+ :param sender_queue_size: Set the maximum number of packets to queue for the sender. Optional
432+ How may packets to queue before blocking or dropping the packet if the packet queue is already full.
433+ Default: 0 (unlimited).
434+ :type sender_queue_size: integer
435+
436+ :param sender_queue_timeout: Set timeout for packet queue operations, in seconds. Optional.
437+ How long the application thread is willing to wait for the queue clear up before dropping the metric packet.
438+ If set to None, wait forever.
439+ If set to zero drop the packet immediately if the queue is full.
440+ Default: 0 (no wait)
441+ :type sender_queue_timeout: float
442+ """
443+
444+ # Avoid a race on _queue with the background buffer flush thread that reads _queue.
445+ with self ._buffer_lock :
446+ if self ._queue is not None :
447+ return
448+
418449 self ._queue = queue .Queue (sender_queue_size )
419450 self ._start_sender_thread ()
420451 if sender_queue_timeout is None :
@@ -559,6 +590,18 @@ def get_socket(self, telemetry=False):
559590
560591 return self .socket
561592
593+ def set_socket_timeout (self , timeout ):
594+ """
595+ Set timeout for socket operations, in seconds.
596+
597+ If set to zero, never wait if operation can not be completed immediately. If set to None, wait forever.
598+ This option does not affect hostname resolution when using UDP.
599+ """
600+ with self ._socket_lock :
601+ self .socket_timeout = timeout
602+ if self .socket :
603+ self .socket .settimeout (timeout )
604+
562605 @classmethod
563606 def _ensure_min_send_buffer_size (cls , sock , min_size = MIN_SEND_BUFFER_SIZE ):
564607 # Increase the receiving buffer size where needed (e.g. MacOS has 4k RX
0 commit comments