Skip to content

Commit

Permalink
Closes #12: Use thread local storage for SeqRepo
Browse files Browse the repository at this point in the history
Limit number of SeqRepo instances
  • Loading branch information
reece authored Nov 13, 2023
2 parents 26aa654 + a993a80 commit e4124b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package_dir =
zip_safe = True

install_requires =
biocommons.seqrepo>=0.6.2
biocommons.seqrepo~=0.6
coloredlogs
connexion[swagger-ui]>=2.2
connexion[swagger-ui]~=2.2
Flask~=2.2

[options.extras_require]
dev =
Expand Down
14 changes: 7 additions & 7 deletions src/seqrepo_rest_service/threadglobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
"""

import logging
import os

from biocommons.seqrepo import SeqRepo
from flask import current_app, g
from flask import current_app

_logger = logging.getLogger(__name__)


def get_seqrepo():
seqrepo_dir = current_app.config["seqrepo_dir"]
_logger.info(f"Opening {seqrepo_dir=}")
if _get_or_create("seqrepo", None, False) is None:
_logger.info("Opening seqrepo_dir=%s", seqrepo_dir)
return _get_or_create("seqrepo", lambda: SeqRepo(root_dir=seqrepo_dir))


def _get_or_create(k, f):
def _get_or_create(k, f, create=True):
k = "_" + k
o = getattr(g, k, None)
if o is None:
o = getattr(_get_or_create, k, None)
if o is None and create:
o = f()
setattr(g, k, o)
setattr(_get_or_create, k, o)
return o

0 comments on commit e4124b9

Please sign in to comment.