aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh B <[email protected]>2020-01-21 23:44:28 +1000
committerRapptz <[email protected]>2020-01-21 20:28:14 -0500
commitd0a1956be98b5e985dcfb77dc22b96198869cde1 (patch)
treef9140b31d1779a36b46a1de20567411d26160904
parent[commands] Only clean semaphore when there are no waiters (diff)
downloaddiscord.py-d0a1956be98b5e985dcfb77dc22b96198869cde1.tar.xz
discord.py-d0a1956be98b5e985dcfb77dc22b96198869cde1.zip
Improve usability of utils.sleep_until
Fix issue where sleeping for an extended period on python 3.5 would cause an exception Add sleep_until to API docs Add result argument to sleep_until
-rw-r--r--discord/ext/tasks/__init__.py6
-rw-r--r--discord/utils.py10
-rw-r--r--docs/api.rst1
3 files changed, 9 insertions, 8 deletions
diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py
index 0ea6e2d0..b33a1b0c 100644
--- a/discord/ext/tasks/__init__.py
+++ b/discord/ext/tasks/__init__.py
@@ -8,8 +8,6 @@ import logging
from discord.backoff import ExponentialBackoff
-MAX_ASYNCIO_SECONDS = 3456000
-
log = logging.getLogger(__name__)
class Loop:
@@ -360,10 +358,6 @@ class Loop:
"""
sleep = seconds + (minutes * 60.0) + (hours * 3600.0)
- if sleep >= MAX_ASYNCIO_SECONDS:
- fmt = 'Total number of seconds exceeds asyncio imposed limit of {0} seconds.'
- raise ValueError(fmt.format(MAX_ASYNCIO_SECONDS))
-
if sleep < 0:
raise ValueError('Total number of seconds cannot be less than zero.')
diff --git a/discord/utils.py b/discord/utils.py
index 49ca2e5c..f9fc20ac 100644
--- a/discord/utils.py
+++ b/discord/utils.py
@@ -43,6 +43,7 @@ from .errors import InvalidArgument
from .object import Object
DISCORD_EPOCH = 1420070400000
+MAX_ASYNCIO_SECONDS = 3456000
class cached_property:
def __init__(self, function):
@@ -338,7 +339,7 @@ async def sane_wait_for(futures, *, timeout):
return done
-async def sleep_until(when):
+async def sleep_until(when, result=None):
"""Sleep until a specified time.
If the time supplied is in the past this function will yield instantly.
@@ -347,6 +348,8 @@ async def sleep_until(when):
-----------
when: :class:`datetime.datetime`
The timestamp in which to sleep until.
+ result: Any
+ If provided is returned to the caller when the coroutine completes.
.. versionadded:: 1.3
"""
@@ -354,7 +357,10 @@ async def sleep_until(when):
when = when.replace(tzinfo=datetime.timezone.utc)
now = datetime.datetime.now(datetime.timezone.utc)
delta = (when - now).total_seconds()
- await asyncio.sleep(max(delta, 0))
+ while delta > MAX_ASYNCIO_SECONDS:
+ await asyncio.sleep(MAX_ASYNCIO_SECONDS)
+ delta -= MAX_ASYNCIO_SECONDS
+ return await asyncio.sleep(max(delta, 0), result)
def valid_icon_size(size):
"""Icons must be power of 2 within [16, 4096]."""
diff --git a/docs/api.rst b/docs/api.rst
index e08b415e..6c257a5b 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -725,6 +725,7 @@ Utility Functions
.. autofunction:: discord.utils.resolve_invite
+.. autofunction:: discord.utils.sleep_until
Profile
---------