bpo-32311: Implement asyncio.create_task() shortcut#4848
bpo-32311: Implement asyncio.create_task() shortcut#4848asvetlov merged 16 commits intopython:masterfrom
Conversation
3876d5a to
67c3520
Compare
|
Working on tests and docs |
| Return a Task object. | ||
| """ | ||
| loop = get_running_loop() | ||
| return loop.create_task(coro) |
There was a problem hiding this comment.
I think this function should be defined in tasks.py.
| Return a Task object. | ||
| """ | ||
| loop = get_running_loop() | ||
| return loop.create_task(coro) |
There was a problem hiding this comment.
What if you pass a Task instance to this function? I'd say we check if coro is a coroutine here and raise an error if it's not.
There was a problem hiding this comment.
Isn't loop.create_task() perform the check?
Actually python implementation has it but C has not.
There was a problem hiding this comment.
Well, Python version has only an assert statement... Let's have an always working check in tasks.py/Task and _asynciomodule.c/Task. It will add a bit of overhead, but will improve the usability.
_asynciomodule.c/Task can have a fast path using PyCoro_CheckExact.
|
BTW, do you need my help with #4799? |
|
I stuck on #4799 (comment) |
| __all__ = (base_events.__all__ + | ||
| coroutines.__all__ + | ||
| events.__all__ + | ||
| format_helpers.__all__ + |
There was a problem hiding this comment.
I've no idea why we export asyncio.extract_stack. Let's stop doing that. Also, why renaming file as part of this PR?
There was a problem hiding this comment.
My mistake -- extract_stack was implemented in events.py but not exported.
utils.py was not exist, functions was defined in events.py.
But it brings circular import dependency when I've added iscoroutine() check to C Task:
iscoroutine()implemented incoroutines.pycoroutines.pyimportsevents.pyevents.pyimports_asyncioforget_running_loop()C optimization_asyncioimportscoroutines.py
That's why I've extracted stack/traceback formatting functions into utils.py but after that renamed utils.py to format_helpers.py.
| self.assertEqual(1, ret) | ||
|
|
||
| self.loop.run_until_complete(coro()) | ||
|
|
There was a problem hiding this comment.
Need a test that would test the "%R is not a coroutine" error.
| } | ||
| if (tmp) { | ||
| self->task_log_destroy_pending = 0; | ||
| PyErr_Format(PyExc_TypeError, "%R is not a coroutine", coro, NULL); |
There was a problem hiding this comment.
Can we change the error message to "a coroutine was expected, got %R" in both C/Python implementations?
| .. function:: create_task(coro) | ||
|
|
||
| Wrap a :ref:`coroutine <coroutine>` *coro* into a task and schedule | ||
| its execution. |
There was a problem hiding this comment.
Wrap a :ref:`coroutine <coroutine>` *coro* into a task and schedule
its execution. Return the task object.
| # Number of stack entries to capture in debug mode. | ||
| # The larger the number, the slower the operation in debug mode | ||
| # (see extract_stack() in events.py). | ||
| # (see extract_stack() in utils.py). |
| return func_repr | ||
|
|
||
|
|
||
| def extract_stack(f=None, limit=None): |
There was a problem hiding this comment.
Can we keep extract_stack in events.py?
There was a problem hiding this comment.
No. extract_stack is used in coroutines.py which in turn is used by _asynciomodule.c.
https://bugs.python.org/issue32311