Skip to content

Instantly share code, notes, and snippets.

@theferrit32
Last active September 7, 2023 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theferrit32/7080f884d27e84701239aacb0f54ad82 to your computer and use it in GitHub Desktop.
Save theferrit32/7080f884d27e84701239aacb0f54ad82 to your computer and use it in GitHub Desktop.
cpython non-immediate GC
from threading import Lock
from functools import lru_cache
import shutil
import tempfile
counter = 0
counter_lock = Lock()
class TestFile(object):
def __init__(self, path, mode="r"):
self.path = path
self.fh = open(path, mode)
def __del__(self):
global counter_lock, counter
with counter_lock:
counter += 1
class TestGC(object):
def __init__(self, id, file_count, mode="r"):
self.id = id
self.open_files = []
self.indexes = []
self.dir = tempfile.mkdtemp()
for i in range(file_count):
f = self._open(f"{self.dir}/test-file-{id}-{i}", mode)
self.open_files.append(f)
self.indexes.append(i)
@lru_cache()
def _open(self, path, mode):
return TestFile(path, mode)
def __del__(self):
shutil.rmtree(self.dir)
obj_count = 500
files_per_obj = 100
for i in range(obj_count):
A = TestGC(str(i), file_count=files_per_obj, mode="w")
del A
print(counter)
assert obj_count * files_per_obj == counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment