Skip to content

Commit

Permalink
fix threading bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rickwierenga committed Aug 24, 2023
1 parent 8298dcb commit 85c9125
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_get_index_html(self):
""" Test that the index.html file is returned. """
r = requests.get("http://localhost:1337/", timeout=10)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.headers["Content-Type"], "text/html")
self.assertIn(r.headers["Content-Type"], ["text/html", "text/html; charset=utf-8"])

async def test_connect(self):
await self.client.send('{"event": "ready"}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SimulatorBackend(WebSocketBackend):
>>> from pylabrobot.liquid_handling.liquid_handler import LiquidHandler
>>> sb = simulation.SimulatorBackend()
>>> lh = LiquidHandler(backend=sb)
>>> lh.setup()
>>> await lh.setup()
INFO:websockets.server:server listening on 127.0.0.1:2121
INFO:pyhamilton.liquid_handling.backends.simulation.simulation:Simulation server started at
http://127.0.0.1:2121
Expand Down Expand Up @@ -127,11 +127,11 @@ def _run_file_server(self):

def start_server():
# try to start the server. If the port is in use, try with another port until it succeeds.
og_path = os.getcwd()
os.chdir(path) # only within thread.

class QuietSimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
""" A simple HTTP request handler that does not log requests. """
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=path, **kwargs)

def log_message(self, format, *args):
# pylint: disable=redefined-builtin
pass
Expand All @@ -148,8 +148,6 @@ def log_message(self, format, *args):

self.httpd.serve_forever()

os.chdir(og_path)

self._fst = threading.Thread(name="simulation_fs", target=start_server, daemon=True)
self.fst.start()

Expand Down
2 changes: 1 addition & 1 deletion pylabrobot/liquid_handling/backends/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def start_loop():
lock = threading.Lock()
lock.acquire() # pylint: disable=consider-using-with
self._loop = asyncio.new_event_loop()
self._t = threading.Thread(target=start_loop)
self._t = threading.Thread(target=start_loop, daemon=True)
self.t.start()

while lock.locked():
Expand Down
2 changes: 1 addition & 1 deletion pylabrobot/liquid_handling/liquid_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def setup(self):
for resource in self.deck.children:
self.resource_assigned_callback(resource)

super().setup()
await super().setup()

def update_head_state(self, state: Dict[int, Optional[Tip]]):
""" Update the state of the liquid handler head.
Expand Down
6 changes: 4 additions & 2 deletions pylabrobot/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def setup_finished(self) -> bool:
return self._setup_finished

async def setup(self):
self.backend.setup()
await self.backend.setup()
self._setup_finished = True

@need_setup_finished
async def stop(self):
self.backend.stop()
await self.backend.stop()
self._setup_finished = False
4 changes: 3 additions & 1 deletion pylabrobot/server/liquid_handling_api_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import cast
import unittest

Expand Down Expand Up @@ -53,6 +54,7 @@ def test_setup(self): # TODO: Figure out how we can configure LH

self.assertIn(response.json.get("status"), {"running", "succeeded"})

time.sleep(0.1)
assert self.lh.setup_finished

def test_stop(self):
Expand Down Expand Up @@ -111,6 +113,7 @@ def setUp(self) -> None:
assert self.lh.deck.resources == deck.resources

client.post(self.base_url + "/setup")
time.sleep(0.5)

def test_tip_pickup(self):
with self.app.test_client() as client:
Expand All @@ -125,7 +128,6 @@ def test_tip_pickup(self):
"tip": serialize(tip),
"offset": None,
}], "use_channels": [0]})
print(response)
self.assertIn(response.json.get("status"), {"running", "succeeded"})
self.assertEqual(response.status_code, 200)

Expand Down

0 comments on commit 85c9125

Please sign in to comment.