Skip to content

Commit 6784deb

Browse files
committed
log warning if user calls time.sleep(); replace with asyncio.sleep()
1 parent d136640 commit 6784deb

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

custom_components/pyscript/eval.py

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import keyword
1212
import logging
1313
import sys
14+
import time
1415
import weakref
1516

1617
import yaml
@@ -1930,6 +1931,13 @@ async def call_func(self, func, func_name, *args, **kwargs):
19301931
if asyncio.iscoroutinefunction(func):
19311932
return await func(*args, **kwargs)
19321933
if callable(func):
1934+
if func == time.sleep: # pylint: disable=comparison-with-callable
1935+
_LOGGER.warning(
1936+
"%s line %s calls blocking time.sleep(); replaced with asyncio.sleep()",
1937+
self.filename,
1938+
self.lineno,
1939+
)
1940+
return await asyncio.sleep(*args, **kwargs)
19331941
return func(*args, **kwargs)
19341942
raise TypeError(f"'{func_name}' is not callable (got {func})")
19351943

tests/test_function.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,8 @@ async def test_service_call_blocking(hass, caplog):
12671267
None,
12681268
[dt(2020, 7, 1, 12, 0, 0, 0)],
12691269
"""
1270+
import time
1271+
12701272
seq_num = 0
12711273
12721274
@time_trigger("startup")
@@ -1298,7 +1300,7 @@ def short_sleep():
12981300
global long_sleep_id
12991301
13001302
long_sleep_id = task.current_task()
1301-
task.sleep(0.0001)
1303+
time.sleep(0.0001)
13021304
pyscript.var1 = int(pyscript.var1) + 1
13031305
13041306
@service(supports_response = "optional")

0 commit comments

Comments
 (0)