Skip to content

Commit 1c97565

Browse files
committed
Linting fixes
1 parent 671d403 commit 1c97565

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

src/labthings/actions/pool.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def start(self, thread: ActionThread):
2929
self.add(thread)
3030
thread.start()
3131

32-
def spawn(self, action: str, function, http_error_lock=None, *args, **kwargs):
32+
def spawn(self, action: str, function, *args, http_error_lock=None, **kwargs):
3333
"""
3434
3535
:param function:
@@ -38,11 +38,11 @@ def spawn(self, action: str, function, http_error_lock=None, *args, **kwargs):
3838
3939
"""
4040
thread = ActionThread(
41-
action,
42-
target=function,
43-
http_error_lock=http_error_lock,
44-
args=args,
45-
kwargs=kwargs
41+
action,
42+
target=function,
43+
http_error_lock=http_error_lock,
44+
args=args,
45+
kwargs=kwargs,
4646
)
4747
self.start(thread)
4848
return thread

src/labthings/actions/thread.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ActionThread(threading.Thread):
4040
* The exception appearing in the logs with a traceback
4141
* The exception being raised in the background thread.
4242
However, `HTTPException` subclasses are used in Flask/Werkzeug web apps to
43-
return HTTP status codes indicating specific errors, and so merit being
43+
return HTTP status codes indicating specific errors, and so merit being
4444
handled differently.
4545
4646
Normally, when an Action is initiated, the thread handling the HTTP request
@@ -49,14 +49,14 @@ class ActionThread(threading.Thread):
4949
in the Action thread before the initiating thread has sent an HTTP response,
5050
we **don't** want to propagate the error here, but instead want to re-raise
5151
it in the calling thread. This will then mean that the HTTP request is
52-
answered with the appropriate error code, rather than returning a `201`
52+
answered with the appropriate error code, rather than returning a `201`
5353
code, along with a description of the task (showing that it was successfully
5454
started, but also showing that it subsequently failed with an error).
5555
56-
In order to activate this behaviour, we must pass in a `threading.Lock`
56+
In order to activate this behaviour, we must pass in a `threading.Lock`
5757
object. This lock should already be acquired by the request-handling
5858
thread. If an error occurs, and this lock is acquired, the exception
59-
should not be re-raised until the calling thread has had the chance to deal
59+
should not be re-raised until the calling thread has had the chance to deal
6060
with it.
6161
"""
6262

@@ -70,7 +70,7 @@ def __init__(
7070
daemon: bool = True,
7171
default_stop_timeout: int = 5,
7272
log_len: int = 100,
73-
http_error_lock: Optional[threading.Lock] = None
73+
http_error_lock: Optional[threading.Lock] = None,
7474
):
7575
threading.Thread.__init__(
7676
self,
@@ -271,8 +271,6 @@ def wrapped(*args, **kwargs):
271271
)
272272
logging.error(traceback.format_exc())
273273
raise e
274-
else:
275-
logging.info(f"Propagating {e} back to request handler")
276274
except Exception as e: # skipcq: PYL-W0703
277275
self._exception = e
278276
logging.error(traceback.format_exc())
@@ -285,7 +283,6 @@ def wrapped(*args, **kwargs):
285283
self._return_value = str(self._exception)
286284
self._status = "error"
287285

288-
289286
return wrapped
290287

291288
def get(self, block: bool = True, timeout: Optional[int] = None):

src/labthings/views/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,14 @@ def dispatch_request(self, *args, **kwargs):
217217
pool = (
218218
current_labthing().actions if current_labthing() else self._emergency_pool
219219
)
220-
error_lock = threading.RLock() # This indicates that we'll handle errors for now
220+
# We pass in this lock to tell the Action thread that we'll deal
221+
# with HTTP errors in this thread
222+
error_lock = threading.RLock()
221223
with error_lock:
222224
# Make a task out of the views `post` method
223-
task = pool.spawn(self.endpoint, meth, *args, http_error_lock=error_lock, **kwargs)
225+
task = pool.spawn(
226+
self.endpoint, meth, *args, http_error_lock=error_lock, **kwargs
227+
)
224228
# Optionally override the threads default_stop_timeout
225229
if self.default_stop_timeout is not None:
226230
task.default_stop_timeout = self.default_stop_timeout
@@ -234,7 +238,6 @@ def dispatch_request(self, *args, **kwargs):
234238
except TimeoutError:
235239
pass
236240

237-
238241
# If the action returns quickly, and returns a valid Response, return it as-is
239242
if task.output and isinstance(task.output, ResponseBase):
240243
return self.represent_response((task.output, 200))

tests/test_action_api.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,24 @@ def test_action_abort(thing_with_some_views, client):
4343
r = client.post("/AbortAction")
4444
assert r.status_code == 418
4545

46+
4647
@pytest.mark.filterwarnings("ignore:Exception in thread")
4748
def test_action_abort_late(thing_with_some_views, client, caplog):
4849
"""Check HTTPExceptions raised late are just regular errors."""
4950
caplog.set_level(logging.ERROR)
5051
caplog.clear()
5152
r = client.post("/AbortAction", data=json.dumps({"abort_after": 0.2}))
52-
assert r.status_code == 201 # Should have started OK
53+
assert r.status_code == 201 # Should have started OK
5354
time.sleep(0.3)
5455
# Now check the status - should be error
5556
r2 = client.get(r.get_json()["links"]["self"]["href"])
5657
assert r2.get_json()["status"] == "error"
5758
# Check it was logged as well
5859
error_was_raised = False
5960
for r in caplog.records:
60-
if (
61-
r.levelname == "ERROR"
62-
and "HTTPException" in r.message
63-
):
61+
if r.levelname == "ERROR" and "HTTPException" in r.message:
6462
error_was_raised = True
6563
assert error_was_raised
66-
6764

6865

6966
def test_action_validate(thing_with_some_views, client):

0 commit comments

Comments
 (0)