summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/tasks.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2017-12-15 05:04:38 (GMT)
committerGitHub <noreply@github.com>2017-12-15 05:04:38 (GMT)
commitf74ef458ab1f502e4e60bd1502ac1dc0d2cb3847 (patch)
tree7c622fa99ba081586a655b1744dae0a46b3a0f95 /Lib/asyncio/tasks.py
parent19a44f63c738388ef3c8515348b4ffc061dfd627 (diff)
downloadcpython-f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847.zip
cpython-f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847.tar.gz
cpython-f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847.tar.bz2
bpo-32311: Implement asyncio.create_task() shortcut (#4848)
* Implement functionality * Add documentation
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r--Lib/asyncio/tasks.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index c5122f7..172057e 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -1,7 +1,7 @@
"""Support for tasks, coroutines and the scheduler."""
__all__ = (
- 'Task',
+ 'Task', 'create_task',
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
'wait', 'wait_for', 'as_completed', 'sleep',
'gather', 'shield', 'ensure_future', 'run_coroutine_threadsafe',
@@ -67,13 +67,19 @@ class Task(futures.Future):
return {t for t in cls._all_tasks if t._loop is loop}
def __init__(self, coro, *, loop=None):
- assert coroutines.iscoroutine(coro), repr(coro)
super().__init__(loop=loop)
if self._source_traceback:
del self._source_traceback[-1]
- self._coro = coro
- self._fut_waiter = None
+ if not coroutines.iscoroutine(coro):
+ # raise after Future.__init__(), attrs are required for __del__
+ # prevent logging for pending task in __del__
+ self._log_destroy_pending = False
+ raise TypeError(f"a coroutine was expected, got {coro!r}")
+
self._must_cancel = False
+ self._fut_waiter = None
+ self._coro = coro
+
self._loop.call_soon(self._step)
self.__class__._all_tasks.add(self)
@@ -263,6 +269,15 @@ else:
Task = _CTask = _asyncio.Task
+def create_task(coro):
+ """Schedule the execution of a coroutine object in a spawn task.
+
+ Return a Task object.
+ """
+ loop = events.get_running_loop()
+ return loop.create_task(coro)
+
+
# wait() and as_completed() similar to those in PEP 3148.
FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED