New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async list comprehension (PEP 530) causes SyntaxError in Python 3.6.3 #76013
Comments
$ python3
Python 3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> async def arange(n):
... for i in range(n):
... yield i
...
>>> [i async for i in arange(10)]
File "<stdin>", line 1
[i async for i in arange(10)]
^
SyntaxError: invalid syntax
>>> |
Your async listcomp must also be defined within a coroutine to turn async def arange(n):
for i in range(n):
yield i
async def alistcomp():
return [i async for i in arange(10)] try: print(value) |
Zachary, Thank you for your response. I had the impression that async comprehensions are a bridge between async functions and non-async functions. Is there a wat to use async (and asyncio) and then go back to regular python? Or am I just wishful thinking? :-) For example, it would be nice to start multiple processes through asyncio.create_subprocess_exec (or _shell) await twice and then process all the output in "traditional" (i.e., non-async) Python. Cheers :), |
I have been thinking about my previous comment a bit more. For consistency there should at least be an await somewhere to move back from async land to non-async land. For example:
import asyncio
async def main():
cmds = [['ssh', 'user@host', 'echo {}'.format(i)] for i in range(4)]
creations = [asyncio.create_subprocess_exec(*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
for cmd in cmds]
processes = [await creation for creation in creations]
outputs = [await process.communicate() for process in processes]
print(outputs)
if __name__ == '__main__':
event_loop = asyncio.get_event_loop()
event_loop.run_until_complete(main()) prints
It would be nice if you could somehow return outputs from main() and process the results in a non-async function. There are no async concepts left in outputs after all. But I am not aware of a way you can do this in Python currently. |
That's already provided, just do |
Thank you! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: