use weakref for backrefs so collection doesn't need to be garbage collected

This commit is contained in:
Damien Elmes 2020-03-03 21:25:32 +10:00
parent a8e6fbd0fd
commit ffe6ecf44c
7 changed files with 14 additions and 6 deletions

View file

@ -7,6 +7,7 @@ import copy
import json import json
import operator import operator
import unicodedata import unicodedata
import weakref
from typing import Any, Dict, List, Optional, Set, Tuple, Union from typing import Any, Dict, List, Optional, Set, Tuple, Union
import anki # pylint: disable=unused-import import anki # pylint: disable=unused-import
@ -101,7 +102,7 @@ class DeckManager:
############################################################# #############################################################
def __init__(self, col: anki.storage._Collection) -> None: def __init__(self, col: anki.storage._Collection) -> None:
self.col = col self.col = weakref.proxy(col)
self.decks = {} self.decks = {}
self.dconf = {} self.dconf = {}

View file

@ -10,6 +10,7 @@ import time
import urllib.error import urllib.error
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import weakref
from typing import Any, Callable, List, Optional, Tuple, Union from typing import Any, Callable, List, Optional, Tuple, Union
import anki import anki
@ -42,7 +43,7 @@ class MediaManager:
regexps = soundRegexps + imgRegexps regexps = soundRegexps + imgRegexps
def __init__(self, col: anki.storage._Collection, server: bool) -> None: def __init__(self, col: anki.storage._Collection, server: bool) -> None:
self.col = col self.col = weakref.proxy(col)
if server: if server:
self._dir = None self._dir = None
return return

View file

@ -7,6 +7,7 @@ import copy
import json import json
import re import re
import time import time
import weakref
from typing import Any, Callable, Dict, List, Optional, Tuple, Union from typing import Any, Callable, Dict, List, Optional, Tuple, Union
import anki # pylint: disable=unused-import import anki # pylint: disable=unused-import
@ -91,7 +92,7 @@ class ModelManager:
############################################################# #############################################################
def __init__(self, col: anki.storage._Collection) -> None: def __init__(self, col: anki.storage._Collection) -> None:
self.col = col self.col = weakref.proxy(col)
self.models = {} self.models = {}
self.changed = False self.changed = False

View file

@ -176,12 +176,14 @@ def proto_progress_to_native(progress: pb.Progress) -> Progress:
else: else:
assert_impossible_literal(kind) assert_impossible_literal(kind)
def _on_progress(progress_bytes: bytes) -> bool: def _on_progress(progress_bytes: bytes) -> bool:
progress = pb.Progress() progress = pb.Progress()
progress.ParseFromString(progress_bytes) progress.ParseFromString(progress_bytes)
native_progress = proto_progress_to_native(progress) native_progress = proto_progress_to_native(progress)
return hooks.bg_thread_progress_callback(True, native_progress) return hooks.bg_thread_progress_callback(True, native_progress)
class RustBackend: class RustBackend:
def __init__( def __init__(
self, col_path: str, media_folder_path: str, media_db_path: str, log_path: str self, col_path: str, media_folder_path: str, media_db_path: str, log_path: str

View file

@ -6,6 +6,7 @@ from __future__ import annotations
import itertools import itertools
import random import random
import time import time
import weakref
from heapq import * from heapq import *
from operator import itemgetter from operator import itemgetter
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
@ -30,7 +31,7 @@ class Scheduler:
_burySiblingsOnAnswer = True _burySiblingsOnAnswer = True
def __init__(self, col: anki.storage._Collection) -> None: def __init__(self, col: anki.storage._Collection) -> None:
self.col = col self.col = weakref.proxy(col)
self.queueLimit = 50 self.queueLimit = 50
self.reportLimit = 1000 self.reportLimit = 1000
self.reps = 0 self.reps = 0

View file

@ -7,6 +7,7 @@ import datetime
import itertools import itertools
import random import random
import time import time
import weakref
from heapq import * from heapq import *
from operator import itemgetter from operator import itemgetter
@ -37,7 +38,7 @@ class Scheduler:
revCount: int revCount: int
def __init__(self, col: anki.storage._Collection) -> None: def __init__(self, col: anki.storage._Collection) -> None:
self.col = col self.col = weakref.proxy(col)
self.queueLimit = 50 self.queueLimit = 50
self.reportLimit = 1000 self.reportLimit = 1000
self.dynReportLimit = 99999 self.dynReportLimit = 99999

View file

@ -13,6 +13,7 @@ from __future__ import annotations
import json import json
import re import re
import weakref
from typing import Callable, Collection, Dict, List, Optional, Tuple from typing import Callable, Collection, Dict, List, Optional, Tuple
import anki # pylint: disable=unused-import import anki # pylint: disable=unused-import
@ -26,7 +27,7 @@ class TagManager:
############################################################# #############################################################
def __init__(self, col: anki.storage._Collection) -> None: def __init__(self, col: anki.storage._Collection) -> None:
self.col = col self.col = weakref.proxy(col)
self.tags: Dict[str, int] = {} self.tags: Dict[str, int] = {}
def load(self, json_: str) -> None: def load(self, json_: str) -> None: