Skip to content

Commit

Permalink
Add semaphore logic to tornado histogram endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
annehaley committed Jan 28, 2025
1 parent be4e480 commit 2f029fa
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions large_image/tilesource/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import importlib.util
import json
import os
import threading
import weakref
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from urllib.parse import parse_qs, quote, urlencode, urlparse, urlunparse

import aiohttp
import numpy as np

import large_image
from large_image.exceptions import TileSourceXYZRangeError
from large_image.tilesource.utilities import JSONDict

Expand Down Expand Up @@ -505,9 +507,12 @@ async def get(self) -> None:
kwargs = {k: ast.literal_eval(self.get_argument(k)) for k in self.request.arguments}

def fetch():
histogram = manager.tile_source._unstyled.histogram( # type: ignore[attr-defined]
**kwargs,
).get('histogram', [{}])
if not hasattr(manager, '_histogram_semaphore'):
manager._histogram_semaphore = threading.Semaphore(min(6, large_image.config.cpu_count()))
with manager._histogram_semaphore:
histogram = manager.tile_source._unstyled.histogram( # type: ignore[attr-defined]
**kwargs,
).get('histogram', [{}])
self.write(json.dumps(histogram, cls=NumpyEncoder))
self.set_header('Content-Type', 'application/json')

Expand Down

0 comments on commit 2f029fa

Please sign in to comment.