Skip to content

Commit 952ac6c

Browse files
authored
Only acquire schedd if we use it (#23)
* Only acquire schedd if we use it * Use lru_cache for py3.7+ compat
1 parent 4dd8427 commit 952ac6c

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/lpcjobqueue/cluster.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ async def start(self):
9696
def sub():
9797
try:
9898
classads = []
99-
with SCHEDD.transaction() as txn:
99+
with SCHEDD().transaction() as txn:
100100
cluster_id = job.queue(txn, ad_results=classads)
101101

102102
logger.debug(f"ClassAds for job {cluster_id}: {classads}")
103-
SCHEDD.spool(classads)
103+
SCHEDD().spool(classads)
104104
return cluster_id
105105
except htcondor.HTCondorInternalError as ex:
106106
logger.error(str(ex))
@@ -136,7 +136,7 @@ async def close(self):
136136

137137
def check_gone():
138138
try:
139-
return len(SCHEDD.query(f"ClusterId == {self.job_id}")) == 0
139+
return len(SCHEDD().query(f"ClusterId == {self.job_id}")) == 0
140140
except htcondor.HTCondorIOError as ex:
141141
logger.error(str(ex))
142142
return False
@@ -168,7 +168,7 @@ def check_gone():
168168

169169
def stop():
170170
try:
171-
res = SCHEDD.act(
171+
res = SCHEDD().act(
172172
htcondor.JobAction.Remove, f"ClusterId == {self.job_id}"
173173
)
174174
if res["TotalSuccess"] == 1 and res["TotalChangedAds"] == 1:
@@ -195,7 +195,7 @@ def _close_job(cls, job_id):
195195
f"Last-ditch attempt to close HTCondor job {job_id} in finalizer! You should confirm the job exits!"
196196
)
197197
try:
198-
SCHEDD.act(htcondor.JobAction.Remove, f"ClusterId == {job_id}")
198+
SCHEDD().act(htcondor.JobAction.Remove, f"ClusterId == {job_id}")
199199
except htcondor.HTCondorIOError as ex:
200200
logger.error(str(ex))
201201
cls.known_jobs.discard(job_id)

src/lpcjobqueue/schedd.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import concurrent.futures
2+
import functools
23
import logging
34
import os
45
import re
@@ -77,8 +78,8 @@ def acquire_schedd():
7778

7879
# Pick a schedd once on import
7980
# Would prefer one per cluster but there is a quite scary weakref.finalize
80-
# that depends on it
81-
SCHEDD = acquire_schedd()
81+
# that depends on it. Wrap with a memoized getter to avoid setup if not used
82+
SCHEDD = functools.lru_cache(acquire_schedd)
8283
# The htcondor binding has a global lock so there's no point in using more than
8384
# one pool for asyncio run_in_executor
8485
SCHEDD_POOL = concurrent.futures.ThreadPoolExecutor(1)

0 commit comments

Comments
 (0)