3

I'm working with a simple Python script to get my head wrapped around the asyncio module. I'm going through the documentation which can be found here

However, I noticed that my installation of Python 3 (version 3.5.3, installed on a raspberry pi) does not recognize async def, but will recognize @asyncio.coroutine. Thus, my script has changed from the tutorial code to:

import asyncio import datetime @asyncio.coroutine def display_date(loop): end_time = loop.time() + 5.0 while True: print(datetime.datetime.now()) if (loop.time() + 1.0) >= end_time: break await asyncio.sleep(1) loop = asyncio.get_event_loop() # Blocking call which returns when the display_date() coroutine is done loop.run_until_complete(display_date(loop)) loop.close() 

However, I'm running into syntax errors at await asyncio.sleep(1). Is there any reason for this?? It runs fine on my ubuntu machine (which has python 3.5.1)

5
  • You need to use yield from when you use the @asyncio.coroutine decorator. await is only allowed in async def Commented Jan 9, 2018 at 21:42
  • The problem is that async def doesn't seem to be working, even though asyncio is installed (through python3 -m pip install asyncio) Commented Jan 9, 2018 at 22:07
  • Don’t install asyncio with pip. It’s part of Python’s standard library. Commented Jan 10, 2018 at 12:30
  • How are you running your code? Is it possible that you’re using a different version than you think? Commented Jan 10, 2018 at 12:32
  • I'm running my code by running python3 scriptname.py at the terminal, same as I would any other script. Commented Jan 10, 2018 at 13:43

3 Answers 3

2

await is allowed inside async def function only.

Old-styled coroutines marked by @asyncio.coroutine decorator should use yield from syntax.

You have Python 3.5.1, so just use new syntax, e.g.:

import asyncio import datetime

async def display_date(loop): end_time = loop.time() + 5.0 while True: print(datetime.datetime.now()) if (loop.time() + 1.0) >= end_time: break await asyncio.sleep(1) loop = asyncio.get_event_loop() # Blocking call which returns when the display_date() coroutine is done loop.run_until_complete(display_date(loop)) loop.close() 
Sign up to request clarification or add additional context in comments.

Comments

0

async def and await - are newer syntax that avaliable only since Python 3.5. If you Python doesn't recognize async def it won't recognize await either.

I hardly believe that some 3.5.3 version doesn't have this syntax implemented for some reason. It's much higher possibility that you're just using older python version. Check it adding to code something like:

import sys print(sys.version) 

It'll show Python version you're running.

Btw, asyncio is standard lib module, you shouldn't install it with pip at all.

3 Comments

output from print(sys.version) is: >>> print(sys.version) 3.5.3 (default, Jan 19 2017, 14:11:04) [GCC 6.3.0 20170124]
@T.Wallis then you shouldn't have problems with async def.
Exactly, but I found it didn't work anyway. I performed a reflash of the OS on the RPI and that seemed to do it.
0

I performed a re-flash of Raspbian on the belligerent little thing. It appears to work now. The odd thing is that the image is the 2019-11-29 version. Weird.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.