Skip to content

Commit 4933617

Browse files
author
David Erb
committed
tests in separate process
1 parent e217ea1 commit 4933617

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

src/chimpflow_lib/chimp_adapter.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import importlib
2-
import inspect
31
import logging
4-
import os
52
import warnings
63
from pathlib import Path
74
from typing import Dict
@@ -37,7 +34,6 @@ def __init__(self, specification: Dict):
3734
model_path: filename of the pytorch file
3835
num_classes: input to chimp detect, normally always 3
3936
"""
40-
self.__specification = specification
4137

4238
self.__model_path = require(
4339
"specification",
@@ -50,7 +46,7 @@ def __init__(self, specification: Dict):
5046
"num_classes",
5147
)
5248

53-
async def process(
49+
def detect(
5450
self, crystal_well_model: CrystalWellModel
5551
) -> CrystalWellAutolocationModel:
5652
"""
@@ -66,6 +62,9 @@ async def process(
6662
# Filename is full path to the input filename.
6763
filename: Path = Path(crystal_well_model.filename)
6864

65+
# Make a detector object.
66+
# TODO: Arrange ChimpDetector internals so that we only have to load
67+
# the torch model once per server, instead of once per detection request.
6968
detector = ChimpDetector(
7069
self.__model_path,
7170
[str(filename)],

src/chimpflow_lib/miners/direct_poll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async def query_and_chimp(self) -> None:
158158
for well_model in well_models:
159159
# Do the chimp processing.
160160
logger.info(f"starting chimp process for {well_model.filename}")
161-
autolocation_model = await self.__chimp_adapter.process(well_model)
161+
autolocation_model = self.__chimp_adapter.detect(well_model)
162162

163163
# Add to the list for uploading.
164164
autolocation_models.append(autolocation_model)

tests/test_chimp_adapter.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import multiprocessing
23
import uuid
34
import warnings
45

@@ -40,13 +41,30 @@ async def _main_coroutine(self, constants, output_directory):
4041
""" """
4142

4243
# Make a specification for the chimp adapter.
43-
specification = {
44+
self.__specification = {
4445
"model_path": constants["model_path"],
4546
"num_classes": 3,
4647
}
4748

48-
# Make the adapter object which computes the autolocation information.
49-
chimp_adapter = ChimpAdapter(specification)
49+
# Do the work in a separate process.
50+
# TODO: Figure out how to release resources from torchvision in process.
51+
p = multiprocessing.Process(target=self.__process)
52+
p.start()
53+
p.join()
54+
55+
# Do the same thing, but in a separate process.
56+
p = multiprocessing.Process(target=self.__process)
57+
p.start()
58+
p.join()
59+
60+
# ----------------------------------------------------------------------------------------
61+
def __process(self):
62+
chimp_adapter = ChimpAdapter(self.__specification)
63+
64+
self.__run(chimp_adapter)
65+
66+
# ----------------------------------------------------------------------------------------
67+
def __run(self, chimp_adapter):
5068

5169
# Make a well model to serve as the input to the chimp adapter process method.
5270
well_model = CrystalWellModel(
@@ -55,8 +73,8 @@ async def _main_coroutine(self, constants, output_directory):
5573
)
5674

5775
# Process the well image and get the resulting autolocation information.
58-
well_model_autolocation: CrystalWellAutolocationModel = (
59-
await chimp_adapter.process(well_model)
76+
well_model_autolocation: CrystalWellAutolocationModel = chimp_adapter.detect(
77+
well_model
6078
)
6179

6280
assert well_model_autolocation.drop_detected

tests/test_miner.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import logging
3+
import multiprocessing
34
import os
45
import time
56

@@ -34,6 +35,19 @@ def test(self, constants, logging_setup, output_directory):
3435
# Configuration file to use.
3536
configuration_file = "tests/configurations/direct_poll.yaml"
3637

38+
# Do the work in a separate process so that the Service test
39+
# can also be run in the same pytest invocation.
40+
# TODO: Figure out how to release resources from torchvision in process.
41+
p = multiprocessing.Process(
42+
target=self.__process,
43+
args=[constants, configuration_file, output_directory],
44+
)
45+
p.start()
46+
p.join()
47+
48+
# ----------------------------------------------------------------------------------------
49+
def __process(self, constants, configuration_file, output_directory):
50+
3751
MinerTester().main(constants, configuration_file, output_directory)
3852

3953

0 commit comments

Comments
 (0)