Skip to content

Commit cf87743

Browse files
lukebaumanncopybara-github
authored andcommitted
Replace time.sleep with asyncio.sleep in profiling server. Remove a log line that is emitted unnecessarily in unittests.
PiperOrigin-RevId: 878638182
1 parent 2601d08 commit cf87743

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

pathwaysutils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
del _initialize
2121

2222
# When changing this, also update the CHANGELOG.md.
23-
__version__ = "v0.1.6"
23+
__version__ = "v0.1.5"

pathwaysutils/profiling.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
"""Profiling Utilities."""
1515

16+
import asyncio
1617
import dataclasses
1718
import json
1819
import logging
@@ -93,7 +94,7 @@ def _start_pathways_trace_from_profile_request(
9394
try:
9495
_, result_future = _profile_state.executable.call()
9596
result_future.result()
96-
except Exception as e: # pylint: disable=broad-except
97+
except Exception: # pylint: disable=broad-except
9798
_logger.exception("Failed to start trace")
9899
_profile_state.reset()
99100
raise
@@ -194,9 +195,9 @@ class ProfilingConfig:
194195
async def profiling(pc: ProfilingConfig): # pylint: disable=unused-variable
195196
_logger.debug("Capturing profiling data for %s ms", pc.duration_ms)
196197
_logger.debug("Writing profiling data to %s", pc.repository_path)
197-
jax.profiler.start_trace(pc.repository_path)
198-
time.sleep(pc.duration_ms / 1e3)
199-
jax.profiler.stop_trace()
198+
await asyncio.to_thread(jax.profiler.start_trace, pc.repository_path)
199+
await asyncio.sleep(pc.duration_ms / 1e3)
200+
await asyncio.to_thread(jax.profiler.stop_trace)
200201
return {"response": "profiling completed"}
201202

202203
uvicorn.run(app, host="0.0.0.0", port=port, log_level="debug")
@@ -210,7 +211,7 @@ async def profiling(pc: ProfilingConfig): # pylint: disable=unused-variable
210211

211212

212213
def stop_server():
213-
"""Raises an error if there is not an active profiler server but otherwise does nothing.
214+
"""Raises an error if there is no active profiler server.
214215
215216
Pathways profiling servers are not stoppable at this time.
216217
"""

pathwaysutils/test/profiling_test.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ def test_start_trace_log_dir_error(self, log_dir):
186186
profiling.start_trace(log_dir)
187187

188188
def test_lock_released_on_success(self):
189-
"""Tests that the lock is released after successful start_trace and stop_trace."""
189+
"""Tests lock release after successful start and stop trace.
190+
191+
Verifies that the profiling lock is released after both a successful
192+
`start_trace` and `stop_trace` calls.
193+
"""
190194
profiling.start_trace("gs://test_bucket/test_dir")
191195
self.assertFalse(profiling._profile_state.lock.locked())
192196
profiling.stop_trace()
@@ -198,7 +202,9 @@ def test_lock_released_on_start_failure(self):
198202
self.mock_plugin_executable_cls.return_value.call.return_value[1]
199203
)
200204
mock_result.result.side_effect = RuntimeError("start failed")
201-
with self.assertRaisesRegex(RuntimeError, "start failed"):
205+
with self.assertRaisesRegex(
206+
RuntimeError, "start failed"
207+
), mock.patch.object(profiling._logger, "exception"):
202208
profiling.start_trace("gs://test_bucket/test_dir2")
203209
self.assertFalse(profiling._profile_state.lock.locked())
204210

0 commit comments

Comments
 (0)