【发布时间】:2018-09-18 05:22:26
【问题描述】:
我想在 Python 3.7 中创建一个多进程理解。
这是我的代码:
async def _url_exists(url):
"""Check whether a url is reachable"""
request = requests.get(url)
return request.status_code == 200:
async def _remove_unexisting_urls(rows):
return {row for row in rows if await _url_exists(row[0])}
rows = [
'http://example.com/',
'http://example.org/',
'http://foo.org/',
]
rows = asyncio.run(_remove_unexisting_urls(rows))
在此代码示例中,我想从列表中删除不存在的 URL。 (请注意,我使用的是集合而不是列表,因为我还想删除重复项)。
我的问题是我仍然看到执行是顺序的。 HTTP 请求使执行等待。 与串行执行相比,执行时间是相同的。
- 我做错了吗?
- 这些 await/async 关键字应该如何与 python 理解一起使用?
【问题讨论】:
标签: python parallel-processing list-comprehension python-asyncio set-comprehension