python新手。试图从https://docs.python.org/3/library/asyncio-task.html运行以下示例程序,但出现错误
$ python coro.py
File "coro.py", line 3
async def compute(x, y):
^
SyntaxError: invalid syntax
以下是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()
我在Ubuntu 14.04中运行python 2.7.6。
怀疑我没有asyncio,我试图安装它(对以前的代码片段没有任何影响),得到了有趣的结果,我真的不知道它是否有效< / p>
# 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...
答案 0 :(得分:3)
来自https://docs.python.org/3/library/asyncio-task.html:
在Python 3.5中添加了协同异步def协同类型
- &GT;你说你正在使用Python 2.7,它不会起作用
答案 1 :(得分:2)
看起来只有Python 3支持异步,as per this documentation