This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: '_UnixSelectorEventLoop' object has no attribute 'create_task'
Type: Stage:
Components: asyncio, Documentation, macOS Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, gvanrossum, pydanny, python-dev, vstinner, yselivanov
Priority: normal Keywords: patch

Created on 2014-07-31 02:40 by pydanny, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Screen Shot 2014-07-30 at 8.13.12 PM.png pydanny, 2014-07-31 03:10
doc_create_task.patch vstinner, 2014-08-02 11:19 review
Messages (10)
msg224374 - (view) Author: Daniel Greenfeld (pydanny) Date: 2014-07-31 02:40
Problem
========

The documentation on asyncio provides an example of a parallel execution of tasks. The code is at: https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks

``` python
import asyncio
 
@asyncio.coroutine
def factorial(name, number):
    f = 1
    for i in range(2, number+1):
        print("Task %s: Compute factorial(%s)..." % (name, i))
        yield from asyncio.sleep(1)
        f *= i
    print("Task %s: factorial(%s) = %s" % (name, number, f))
 
loop = asyncio.get_event_loop()
tasks = [
    loop.create_task(factorial("A", 2)),
    loop.create_task(factorial("B", 3)),
    loop.create_task(factorial("C", 4))]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
```

Unfortunately, when I try to run this sample code on Python 3.4.1, I run into an error. Specifically, the `loop.create_task()` method does not exist:

``` python
Traceback (most recent call last):
  File "what_me_asynicio.py", line 14, in <module>
    loop.create_task(factorial("A", 2)),
AttributeError: '_UnixSelectorEventLoop' object has no attribute 'create_task'
```

When I perform a dir() on the `loop` object, no `create_task` item is in the result.

System Information
====================

Python Version: 3.4.1
Operating System: OSX 10.9.3
(Also confirmed on Python 3.4.0 in Ubuntu 14.04)
msg224376 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-31 02:47
Thanks for reporting a doc bug!

The code should use asyncio.async() instead of loop.create_task().
msg224377 - (view) Author: Daniel Greenfeld (pydanny) Date: 2014-07-31 03:10
This has been fixed in 3.4.2, but shows up in the 3.4.1 documentation.

How do I fix the 3.4.1 documentation so this doesn't show up?
msg224378 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-31 03:48
I honestly don't know; ask Benjamin. :-)

Personally, I'd say that ship has sailed.
msg224388 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-31 09:04
> This has been fixed in 3.4.2, but shows up in the 3.4.1 documentation.

Well, I didn't want to change Python 3.4.1 documentation, but it looks like https://docs.python.org/3.4/ is updated after each commit into the 3.4 branch. For example, new asyncio functions added in 3.4.2 are already documented:

https://docs.python.org/3.4/library/asyncio-eventloop.html#asyncio.BaseEventLoop.create_task

I changeed all examples to use create_task() instaed of async() or the Task contructor:

changeset:   91609:66f06fbf8a2f
branch:      3.4
user:        Victor Stinner <victor.stinner@gmail.com>
date:        Tue Jul 08 12:39:10 2014 +0200
files:       Doc/library/asyncio-dev.rst Doc/library/asyncio-eventloop.rst Doc/library/asyncio-stream.r
description:
Update asyncio documentation

- Document the new create_task() method
- "Hide" the Task class: point to the create_task() method for interoperability
- Rewrite the documentation of the Task class
- Document the "Pending task destroyed"
- Update output in debug mode of examples in the dev section
- Replace Task() with create_task() in examples


Maybe I should revert this change in the 3.4 branch, but mention that Python 3.4.2 and 3.5 have a new create_task() which is now the recommanded way to schedule a coroutine (to create a task object).
msg224427 - (view) Author: Daniel Greenfeld (pydanny) Date: 2014-07-31 16:51
First, if there is documentation that says, "3.4.1", doesn't it make sense that the documentation should only be for 3.4.1? Which means that this create_task documentation should be reverted in the 3.4.1 documentation to match the 3.4.1 specification.

Second and most respectfully, why is a feature being added in 3.4.2? I thought this kind of release (3.4.1 to 3.4.2) was for bug fixes and security issues, not new features. Unless create_task was in the original 3.4 specification, I argue that create_task belongs in 3.5. In it's place, a recipe of running tasks in parallel fashion per gvanrossum's suggestion of asyncio.async() could be added.
msg224429 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-31 17:11
asyncio has an explicit exemption from the general rule that bugfixes
should not add new features. This is because of the "provisional" status of
the PEP. We'll stop doing this once 3.5 is out.

I don't know what's up with the online docs.

On Thu, Jul 31, 2014 at 9:51 AM, Daniel Greenfeld <report@bugs.python.org>
wrote:

>
> Daniel Greenfeld added the comment:
>
> First, if there is documentation that says, "3.4.1", doesn't it make sense
> that the documentation should only be for 3.4.1? Which means that this
> create_task documentation should be reverted in the 3.4.1 documentation to
> match the 3.4.1 specification.
>
> Second and most respectfully, why is a feature being added in 3.4.2? I
> thought this kind of release (3.4.1 to 3.4.2) was for bug fixes and
> security issues, not new features. Unless create_task was in the original
> 3.4 specification, I argue that create_task belongs in 3.5. In it's place,
> a recipe of running tasks in parallel fashion per gvanrossum's suggestion
> of asyncio.async() could be added.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue22112>
> _______________________________________
>
msg224553 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-08-02 11:19
Here is a a patch which replaces loop.create_task(coro) with asyncio.async(coro), mention that asyncio.async() can be used to scheduler a coroutine, and make it clear that create_task() is only available in Python 3.4.2 and later.

Does it look better?

If it's possible, I would prefer to have exactly the same documentation in Python 3.4 and 3.5.
msg224580 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-08-02 18:48
Looks good!

On Sat, Aug 2, 2014 at 4:19 AM, STINNER Victor <report@bugs.python.org>
wrote:

>
> STINNER Victor added the comment:
>
> Here is a a patch which replaces loop.create_task(coro) with
> asyncio.async(coro), mention that asyncio.async() can be used to scheduler
> a coroutine, and make it clear that create_task() is only available in
> Python 3.4.2 and later.
>
> Does it look better?
>
> If it's possible, I would prefer to have exactly the same documentation in
> Python 3.4 and 3.5.
>
> ----------
> keywords: +patch
> Added file: http://bugs.python.org/file36203/doc_create_task.patch
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue22112>
> _______________________________________
>
msg225170 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-08-10 23:11
New changeset d0ea92701b1e by Victor Stinner in branch '3.4':
Issue #22112, asyncio doc: replace loop.create_task(coro) with
http://hg.python.org/cpython/rev/d0ea92701b1e

New changeset 18a311479e8b by Victor Stinner in branch 'default':
(Merge 3.4) Issue #22112, asyncio doc: replace loop.create_task(coro) with
http://hg.python.org/cpython/rev/18a311479e8b
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66310
2014-08-10 23:11:53vstinnersetstatus: open -> closed
resolution: fixed
2014-08-10 23:11:38python-devsetnosy: + python-dev
messages: + msg225170
2014-08-02 18:48:49gvanrossumsetmessages: + msg224580
2014-08-02 11:19:44vstinnersetfiles: + doc_create_task.patch
keywords: + patch
messages: + msg224553
2014-07-31 17:11:15gvanrossumsetmessages: + msg224429
2014-07-31 16:51:32pydannysetmessages: + msg224427
2014-07-31 09:04:33vstinnersetmessages: + msg224388
2014-07-31 03:48:18gvanrossumsetmessages: + msg224378
2014-07-31 03:10:56pydannysetfiles: + Screen Shot 2014-07-30 at 8.13.12 PM.png

messages: + msg224377
2014-07-31 02:47:33gvanrossumsetmessages: + msg224376
2014-07-31 02:40:18pydannycreate