1

New to python. Tried to run the following sample program from https://docs.python.org/3/library/asyncio-task.html but got error

$ python coro.py
  File "coro.py", line 3
    async def compute(x, y):
            ^
SyntaxError: invalid syntax

Here is the code snippet in coro.py

import asyncio

async def compute(x, y):
    print("Compute %s + %s ..." % (x, y))
    await asyncio.sleep(1.0)
    return x + y

async def print_sum(x, y):
    result = await compute(x, y)
    print("%s + %s = %s" % (x, y, result))

loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

I am running python 2.7.6 in Ubuntu 14.04.

Suspecting that I don't have asyncio, I tried to install it (didn't have any effect on previous code snippet), got funny result, I really don't know whether it worked

# pip install asyncio
Downloading/unpacking asyncio
  Downloading asyncio-3.4.3.tar.gz (204kB): 204kB downloaded
  Running setup.py (path:/tmp/pip_build_root/asyncio/setup.py) egg_info for package asyncio

Installing collected packages: asyncio
  Running setup.py install for asyncio
      File "/usr/local/lib/python2.7/dist-packages/asyncio/windows_utils.py", line 83
        def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
                  ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/windows_events.py", line 45
        def __init__(self, ov, *, loop=None):
                                ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/futures.py", line 143
        def __init__(self, *, loop=None):
                            ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/streams.py", line 39
        def open_connection(host=None, port=None, *,
                                                   ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/base_subprocess.py", line 151
        _, pipe = yield from loop.connect_write_pipe(
                           ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/events.py", line 282
        def getaddrinfo(self, host, port, *, family=0, type=0, proto=0, flags=0):
                                           ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/coroutines.py", line 46
        yield from gen
                 ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/queues.py", line 41
        def __init__(self, maxsize=0, *, loop=None):
                                       ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/base_events.py", line 177
        yield from waiter
                 ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/subprocess.py", line 118
        return (yield from self._transport._wait())
                         ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/tasks.py", line 70
        def __init__(self, coro, *, loop=None):
                                  ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/test_utils.py", line 134
        def _run_test_server(*, address, use_ssl=False, server_cls, server_ssl_cls):
                              ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/unix_events.py", line 188
        yield from waiter
                 ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/locks.py", line 96
        def __init__(self, *, loop=None):
                            ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/selectors.py", line 39
        "{!r}".format(fileobj)) from None
                                   ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/proactor_events.py", line 386
        *, server_side=False, server_hostname=None,
         ^
    SyntaxError: invalid syntax

      File "/usr/local/lib/python2.7/dist-packages/asyncio/selector_events.py", line 57
        def _make_socket_transport(self, sock, protocol, waiter=None, *,
                                                                       ^
    SyntaxError: invalid syntax


Successfully installed asyncio
Cleaning up...
1
  • asyncio is available as standard module from Python 3.4 onwards. Commented Apr 9, 2016 at 16:02

2 Answers 2

4

From https://docs.python.org/3/library/asyncio-task.html:

The async def type of coroutine was added in Python 3.5

--> you say you're using Python 2.7, that won't work

Sign up to request clarification or add additional context in comments.

3 Comments

Wonder if there is a way to get python 2.7 working with it.
@codingFun No, there is no way. asyncio is part of the standard library, it's designed to work only with Python 3.4 and above. There are major fundamental differences between how the two are written, you aren't going to get the benefits in Python 2.7. You are welcome to try backporting it, however, but good luck.
Thanks @akshat-mahajan, it makes sense.
2

Looks like async is only supported in Python 3, as per this documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.