reactor: Move garbage collection logic from _check_timers() to new _check_gc()

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor
2026-03-20 14:06:09 -04:00
parent 01f089e710
commit dcadac19b7

View File

@@ -1,6 +1,6 @@
# File descriptor and timer event helper
#
# Copyright (C) 2016-2025 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2016-2026 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os, gc, select, math, time, logging, queue
@@ -109,7 +109,7 @@ class SelectReactor:
self._process = False
self.monotonic = chelper.get_ffi()[1].get_monotonic
# Python garbage collection
self._check_gc = gc_checking
self._gc_checking = gc_checking
self._last_gc_times = [0., 0., 0.]
# Timers
self._timers = []
@@ -130,8 +130,24 @@ class SelectReactor:
self._greenlets = []
self._all_greenlets = []
self._prevent_pause_count = 0
# Python garbage collection
def get_gc_stats(self):
return tuple(self._last_gc_times)
def _check_gc(self, eventtime):
if not self._gc_checking:
return False
gi = gc.get_count()
if gi[0] < 700:
return False
# Reactor looks idle and gc is due - run it
gc_level = 0
if gi[1] >= 10:
gc_level = 1
if gi[2] >= 10:
gc_level = 2
self._last_gc_times[gc_level] = eventtime
gc.collect(gc_level)
return True
# Timers
def update_timer(self, timer_handler, waketime):
if timer_handler.timer_is_running:
@@ -154,18 +170,9 @@ class SelectReactor:
if eventtime < self._next_timer:
if busy:
return 0.
if self._check_gc:
gi = gc.get_count()
if gi[0] >= 700:
# Reactor looks idle and gc is due - run it
gc_level = 0
if gi[1] >= 10:
gc_level = 1
if gi[2] >= 10:
gc_level = 2
self._last_gc_times[gc_level] = eventtime
gc.collect(gc_level)
return 0.
gc_busy = self._check_gc(eventtime)
if gc_busy:
return 0.
return min(1., max(.001, self._next_timer - eventtime))
self._next_timer = self.NEVER
g_dispatch = self._g_dispatch