Skip to content

Commit

Permalink
log warning if user calls time.sleep(); replace with asyncio.sleep()
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarratt committed Jul 30, 2023
1 parent d136640 commit 6784deb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import keyword
import logging
import sys
import time
import weakref

import yaml
Expand Down Expand Up @@ -1930,6 +1931,13 @@ async def call_func(self, func, func_name, *args, **kwargs):
if asyncio.iscoroutinefunction(func):
return await func(*args, **kwargs)
if callable(func):
if func == time.sleep: # pylint: disable=comparison-with-callable
_LOGGER.warning(
"%s line %s calls blocking time.sleep(); replaced with asyncio.sleep()",
self.filename,
self.lineno,
)
return await asyncio.sleep(*args, **kwargs)
return func(*args, **kwargs)
raise TypeError(f"'{func_name}' is not callable (got {func})")

Expand Down
4 changes: 3 additions & 1 deletion tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,8 @@ async def test_service_call_blocking(hass, caplog):
None,
[dt(2020, 7, 1, 12, 0, 0, 0)],
"""
import time
seq_num = 0
@time_trigger("startup")
Expand Down Expand Up @@ -1298,7 +1300,7 @@ def short_sleep():
global long_sleep_id
long_sleep_id = task.current_task()
task.sleep(0.0001)
time.sleep(0.0001)
pyscript.var1 = int(pyscript.var1) + 1
@service(supports_response = "optional")
Expand Down

0 comments on commit 6784deb

Please sign in to comment.