summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshipmints <shipmints@gmail.com>2019-10-14 21:10:10 -0400
committerAlex Grönholm <alex.gronholm@nextday.fi>2019-10-17 20:03:36 +0300
commitd1df878c02defcd14c5a56d4c3a70011744fa219 (patch)
tree98346c5e98422f5cad41fd9fe344d49952b8382f
parent1428971e178f861a5d104e9cbf8ce936384089bc (diff)
downloadapscheduler-d1df878c02defcd14c5a56d4c3a70011744fa219.tar.gz
Properly unwrap partial async functions to direct them to the correct async scheduler.
-rw-r--r--apscheduler/executors/asyncio.py4
-rw-r--r--apscheduler/executors/tornado.py4
-rw-r--r--apscheduler/util.py8
-rw-r--r--tests/test_util.py23
4 files changed, 35 insertions, 4 deletions
diff --git a/apscheduler/executors/asyncio.py b/apscheduler/executors/asyncio.py
index 5139622..1bdeb78 100644
--- a/apscheduler/executors/asyncio.py
+++ b/apscheduler/executors/asyncio.py
@@ -2,7 +2,9 @@ from __future__ import absolute_import
import sys
+
from apscheduler.executors.base import BaseExecutor, run_job
+from apscheduler.util import iscoroutinefunction_partial
try:
from asyncio import iscoroutinefunction
@@ -46,7 +48,7 @@ class AsyncIOExecutor(BaseExecutor):
else:
self._run_job_success(job.id, events)
- if iscoroutinefunction(job.func):
+ if iscoroutinefunction_partial(job.func):
if run_coroutine_job is not None:
coro = run_coroutine_job(job, job._jobstore_alias, run_times, self._logger.name)
f = self._eventloop.create_task(coro)
diff --git a/apscheduler/executors/tornado.py b/apscheduler/executors/tornado.py
index a4696ce..020d207 100644
--- a/apscheduler/executors/tornado.py
+++ b/apscheduler/executors/tornado.py
@@ -14,6 +14,8 @@ except ImportError:
def iscoroutinefunction(func):
return False
+from apscheduler.util import iscoroutinefunction_partial
+
class TornadoExecutor(BaseExecutor):
"""
@@ -44,7 +46,7 @@ class TornadoExecutor(BaseExecutor):
else:
self._run_job_success(job.id, events)
- if iscoroutinefunction(job.func):
+ if iscoroutinefunction_partial(job.func):
f = run_coroutine_job(job, job._jobstore_alias, run_times, self._logger.name)
else:
f = self.executor.submit(run_job, job, job._jobstore_alias, run_times,
diff --git a/apscheduler/util.py b/apscheduler/util.py
index e7b4869..5a3bce9 100644
--- a/apscheduler/util.py
+++ b/apscheduler/util.py
@@ -5,7 +5,7 @@ from __future__ import division
from datetime import date, datetime, time, timedelta, tzinfo
from calendar import timegm
from functools import partial
-from inspect import isclass, ismethod
+from inspect import isclass, ismethod, iscoroutinefunction
import re
from pytz import timezone, utc, FixedOffset
@@ -409,3 +409,9 @@ def check_callable_args(func, args, kwargs):
raise ValueError(
'The target callable does not accept the following keyword arguments: %s' %
', '.join(unmatched_kwargs))
+
+
+def iscoroutinefunction_partial(f):
+ while isinstance(f, partial):
+ f = f.func
+ return iscoroutinefunction(f)
diff --git a/tests/test_util.py b/tests/test_util.py
index f1f07e6..441c60d 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -13,7 +13,9 @@ from apscheduler.job import Job
from apscheduler.util import (
asint, asbool, astimezone, convert_to_datetime, datetime_to_utc_timestamp,
utc_timestamp_to_datetime, timedelta_seconds, datetime_ceil, get_callable_name, obj_to_ref,
- ref_to_obj, maybe_ref, check_callable_args, datetime_repr, repr_escape)
+ ref_to_obj, maybe_ref, check_callable_args, datetime_repr, repr_escape,
+ iscoroutinefunction_partial
+)
from tests.conftest import minpython, maxpython
try:
@@ -355,3 +357,22 @@ class TestCheckCallableArgs(object):
exc = pytest.raises(ValueError, check_callable_args, func, [1], {})
assert str(exc.value) == ('The following keyword-only arguments have not been supplied in '
'kwargs: y')
+
+
+def not_a_coro(x):
+ pass
+
+
+async def a_coro(x):
+ pass
+
+
+class TestIsCoroutineFunctionPartial(object):
+ def test_non_coro(self):
+ assert iscoroutinefunction_partial(not_a_coro) is False
+
+ def test_coro(self):
+ assert iscoroutinefunction_partial(a_coro) is True
+
+ def test_coro_partial(self):
+ assert iscoroutinefunction_partial(partial(a_coro, 1)) is True