Skip to content

Commit 506a026

Browse files
Native threaded concurrency (#120)
* Converted to native threads for concurrency * Added elegant action stopping * Added timeout to Action DELETE requests * Switch to thread concurrency * Updated tests and fixed bugs * Removed assert outside of tests * Autofix issues in 1 files Resolved issues in the following files via DeepSource Autofix: 1. src/labthings/wsgi.py * Fixed legacy import Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
1 parent 5f2da89 commit 506a026

22 files changed

+385
-791
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
branch = True
33
source = ./src/labthings
44
omit = .venv/*, ./src/labthings/wsgi.py, ./src/labthings/monkey.py, ./src/labthings/server/*, ./src/labthings/core/*
5-
concurrency = greenlet
5+
concurrency = thread
66

77
[report]
88
# Regexes for lines to exclude from consideration

examples/components/pdf_component.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import math
33
import time
44

5+
from labthings.tasks import current_task
6+
57
"""
68
Class for our lab component functionality. This could include serial communication,
79
equipment API calls, network requests, or a "virtual" device as seen here.
@@ -44,6 +46,8 @@ def average_data(self, n: int):
4446
summed_data = self.data
4547

4648
for _ in range(n):
49+
if current_task() and current_task().stopped:
50+
return summed_data
4751
summed_data = [summed_data[i] + el for i, el in enumerate(self.data)]
4852
time.sleep(0.25)
4953

poetry.lock

Lines changed: 15 additions & 221 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ marshmallow = "^3.4.0"
1818
webargs = "^6.0.0"
1919
apispec = "^3.2.0"
2020
flask-cors = "^3.0.8"
21-
gevent = ">=1.4,<21.0"
22-
gevent-websocket = "^0.10.1"
2321
zeroconf = ">=0.24.5,<0.29.0"
22+
flask-threaded-sockets = "^0.1.0"
2423

2524
[tool.poetry.dev-dependencies]
2625
pytest = "^5.4"

src/labthings/default_views/actions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from ..view import View
44
from ..view.marshalling import marshal_with
5+
from ..view.args import use_args
56
from ..schema import ActionSchema
67
from ..find import current_thing
8+
from .. import fields
79

810

911
class ActionQueue(View):
@@ -12,7 +14,7 @@ class ActionQueue(View):
1214
"""
1315

1416
def get(self):
15-
return ActionSchema(many=True).dump(current_thing.actions.greenlets)
17+
return ActionSchema(many=True).dump(current_thing.actions.threads)
1618

1719

1820
class ActionView(View):
@@ -38,19 +40,22 @@ def get(self, task_id):
3840

3941
return ActionSchema().dump(task)
4042

41-
def delete(self, task_id):
43+
@use_args({"timeout": fields.Int(missing=5)})
44+
def delete(self, args, task_id):
4245
"""
4346
Terminate a running task.
4447
4548
If the task is finished, deletes its entry.
4649
"""
50+
timeout = args.get("timeout", 5)
4751
task_dict = current_thing.actions.to_dict()
4852

4953
if task_id not in task_dict:
5054
return abort(404) # 404 Not Found
5155

5256
task = task_dict.get(task_id)
5357

54-
task.kill(block=True, timeout=3)
58+
# TODO: Make non-blocking?
59+
task.stop(timeout=timeout)
5560

5661
return ActionSchema().dump(task)

src/labthings/default_views/tasks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get(self):
1818
logging.warning(
1919
"TaskList is deprecated and will be removed in a future version. Use the Actions list instead."
2020
)
21-
return TaskSchema(many=True).dump(current_thing.actions.greenlets)
21+
return TaskSchema(many=True).dump(current_thing.actions.threads)
2222

2323

2424
class TaskView(View):
@@ -64,7 +64,6 @@ def delete(self, task_id):
6464
return abort(404) # 404 Not Found
6565

6666
task = task_dict.get(task_id)
67-
68-
task.kill(block=True, timeout=3)
67+
task.stop(timeout=5)
6968

7069
return TaskSchema().dump(task)

src/labthings/labthing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import url_for
2+
from flask_threaded_sockets.flask import Sockets
23
from apispec import APISpec
34

45
# from apispec.ext.marshmallow import MarshmallowPlugin
@@ -19,7 +20,6 @@
1920
from .representations import DEFAULT_REPRESENTATIONS
2021
from .apispec import MarshmallowPlugin, rule_to_apispec_path
2122
from .td import ThingDescription
22-
from .sockets import Sockets
2323
from .event import Event
2424

2525
from .tasks import Pool
@@ -61,7 +61,7 @@ def __init__(
6161

6262
self.extensions = {}
6363

64-
self.actions = Pool() # Pool of greenlets for Actions
64+
self.actions = Pool() # Pool of threads for Actions
6565

6666
self.events = {}
6767

src/labthings/server/sockets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from ..sockets import Sockets, SocketSubscriber
1+
from ..sockets import SocketSubscriber
2+
from flask_threaded_sockets.flask import Sockets

src/labthings/sockets.py

Lines changed: 0 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
"""
2-
Once upon a time, based on flask-websocket; Copyright (C) 2013 Kenneth Reitz
3-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6-
"""
7-
8-
# -*- coding: utf-8 -*-
9-
10-
from werkzeug.exceptions import NotFound
11-
from werkzeug.http import parse_cookie
12-
from flask import request
13-
14-
from flask.helpers import _endpoint_from_view_func
15-
from werkzeug.routing import Map, Rule, BuildError
16-
171
from .representations import encode_json
182

193

@@ -25,158 +9,3 @@ def emit(self, event: dict):
259
response = encode_json(event)
2610
# TODO: Logic surrounding if this subscriber is subscribed to the requested event type
2711
self.ws.send(response)
28-
29-
30-
class WsUrlAdapterWrapper(object):
31-
def __init__(self, app_adapter, sockets_adapter):
32-
self.__app_adapter = app_adapter
33-
self.__sockets_adapter = sockets_adapter
34-
35-
def build(
36-
self,
37-
endpoint,
38-
values=None,
39-
method=None,
40-
force_external=False,
41-
append_unknown=True,
42-
):
43-
try:
44-
return (
45-
"ws"
46-
+ self.__sockets_adapter.build(
47-
endpoint=endpoint,
48-
values=values,
49-
method=None,
50-
force_external=True,
51-
append_unknown=append_unknown,
52-
)[4:]
53-
)
54-
except BuildError:
55-
return self.__app_adapter.build(
56-
endpoint=endpoint,
57-
values=values,
58-
method=method,
59-
force_external=force_external,
60-
append_unknown=append_unknown,
61-
)
62-
63-
def __getattr__(self, attr):
64-
fun = getattr(self.__app_adapter, attr)
65-
setattr(self, attr, fun)
66-
return fun
67-
68-
69-
class Sockets:
70-
def __init__(self, app=None):
71-
#: Compatibility with 'Flask' application.
72-
#: The :class:`~werkzeug.routing.Map` for this instance. You can use
73-
#: this to change the routing converters after the class was created
74-
#: but before any routes are connected.
75-
self.url_map = Map()
76-
77-
#: Compatibility with 'Flask' application.
78-
#: All the attached blueprints in a dictionary by name. Blueprints
79-
#: can be attached multiple times so this dictionary does not tell
80-
#: you how often they got attached.
81-
self.blueprints = {}
82-
self._blueprint_order = []
83-
84-
self.view_functions = {}
85-
86-
if app:
87-
self.init_app(app)
88-
89-
def __create_url_adapter(self, url_map, request):
90-
if request is not None:
91-
return url_map.bind_to_environ(
92-
request.environ, server_name=self.app.config["SERVER_NAME"]
93-
)
94-
elif self.app.config["SERVER_NAME"] is not None:
95-
return url_map.bind(
96-
self.app.config["SERVER_NAME"],
97-
script_name=self.app.config["APPLICATION_ROOT"] or "/",
98-
url_scheme=self.app.config["PREFERRED_URL_SCHEME"],
99-
)
100-
101-
def create_url_adapter(self, request):
102-
adapter_for_app = self.__create_url_adapter(self.app.url_map, request)
103-
adapter_for_sockets = self.__create_url_adapter(self.url_map, request)
104-
return WsUrlAdapterWrapper(adapter_for_app, adapter_for_sockets)
105-
106-
def init_app(self, app):
107-
self.app = app
108-
self.app_wsgi_app = app.wsgi_app
109-
110-
app.wsgi_app = self.wsgi_app
111-
app.create_url_adapter = self.create_url_adapter
112-
113-
def route(self, rule, **options):
114-
def decorator(f):
115-
endpoint = options.pop("endpoint", None)
116-
self.add_url_rule(rule, endpoint, f, **options)
117-
return f
118-
119-
return decorator
120-
121-
def add_url_rule(self, rule, endpoint, f, **options):
122-
if endpoint is None:
123-
endpoint = _endpoint_from_view_func(f)
124-
125-
methods = options.pop("methods", None)
126-
127-
setattr(f, "endpoint", endpoint)
128-
129-
self.url_map.add(Rule(rule, endpoint=endpoint, **options))
130-
self.view_functions[endpoint] = f
131-
132-
if methods is None:
133-
methods = []
134-
self.app.add_url_rule(rule, endpoint, f, methods=methods, **options)
135-
136-
def add_view(self, url, f, endpoint=None, **options):
137-
return self.add_url_rule(url, endpoint, f, **options)
138-
139-
def register_blueprint(self, blueprint, **options):
140-
"""
141-
Registers a blueprint for web sockets like for 'Flask' application.
142-
Decorator :meth:`~flask.app.setupmethod` is not applied, because it
143-
requires ``debug`` and ``_got_first_request`` attributes to be defined.
144-
"""
145-
first_registration = False
146-
147-
if blueprint.name in self.blueprints:
148-
assert self.blueprints[blueprint.name] is blueprint, (
149-
"A blueprint's name collision occurred between %r and "
150-
'%r. Both share the same name "%s". Blueprints that '
151-
"are created on the fly need unique names."
152-
% (blueprint, self.blueprints[blueprint.name], blueprint.name)
153-
)
154-
else:
155-
self.blueprints[blueprint.name] = blueprint
156-
self._blueprint_order.append(blueprint)
157-
first_registration = True
158-
159-
blueprint.register(self, options, first_registration)
160-
161-
def wsgi_app(self, environ, start_response):
162-
adapter = self.url_map.bind_to_environ(environ)
163-
try:
164-
# Find handler view function
165-
endpoint, values = adapter.match()
166-
handler = self.view_functions[endpoint]
167-
168-
# Handle environment
169-
environment = environ["wsgi.websocket"]
170-
cookie = None
171-
if "HTTP_COOKIE" in environ:
172-
cookie = parse_cookie(environ["HTTP_COOKIE"])
173-
174-
with self.app.app_context():
175-
with self.app.request_context(environ):
176-
# add cookie to the request to have correct session handling
177-
request.cookie = cookie
178-
# Run WebSocket handler
179-
handler(environment, **values)
180-
return []
181-
except (NotFound, KeyError):
182-
return self.app_wsgi_app(environ, start_response)

src/labthings/sync/event.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from gevent.hub import getcurrent
2-
import gevent
31
import time
42
import logging
5-
from gevent.lock import BoundedSemaphore
63

7-
from gevent.event import Event
4+
import threading
5+
from _thread import get_ident
86

97

108
class ClientEvent(object):
@@ -17,25 +15,18 @@ class ClientEvent(object):
1715

1816
def __init__(self):
1917
self.events = {}
20-
self._setting_lock = BoundedSemaphore()
18+
self._setting_lock = threading.Lock()
2119

2220
def wait(self, timeout: int = 5):
2321
"""Wait for the next data frame (invoked from each client's thread)."""
24-
ident = id(getcurrent())
22+
ident = get_ident()
2523
if ident not in self.events:
2624
# this is a new client
2725
# add an entry for it in the self.events dict
2826
# each entry has two elements, a threading.Event() and a timestamp
29-
self.events[ident] = [Event(), time.time()]
27+
self.events[ident] = [threading.Event(), time.time()]
3028

31-
# We have to reimplement event waiting here as we need native thread events to allow gevent context switching
32-
wait_start = time.time()
33-
while not self.events[ident][0].is_set():
34-
now = time.time()
35-
if now - wait_start > timeout:
36-
return False
37-
gevent.sleep(0)
38-
return True
29+
return self.events[ident][0].wait(timeout=timeout)
3930

4031
def set(self, timeout=5):
4132
"""Signal that a new frame is available."""
@@ -61,10 +52,10 @@ def set(self, timeout=5):
6152

6253
def clear(self):
6354
"""Clear frame event, once processed."""
64-
ident = id(getcurrent())
55+
ident = get_ident()
6556
if ident not in self.events:
6657
logging.error(f"Mismatched ident. Current: {ident}, available:")
6758
logging.error(self.events.keys())
6859
return False
69-
self.events[id(getcurrent())][0].clear()
60+
self.events[get_ident()][0].clear()
7061
return True

0 commit comments

Comments
 (0)