AIOFiles 比正常文件操作需要更长的时间
AIOFiles Take Longer Than Normal File Operation
我有一个问题 我是 python 异步世界的新手,我写了一些代码来测试 asyncio
的功能,我创建了 10 个具有随机内容的文件,名为 file1.txt, file2.txt, ..., file10.txt
这是我的代码:
import asyncio
import aiofiles
import time
async def reader(pack, address):
async with aiofiles.open(address) as file:
pack.append(await file.read())
async def main():
content = []
await asyncio.gather(*(reader(content, f'./file{_+1}.txt') for _ in range(10)))
return content
def core():
content = []
for number in range(10):
with open(f'./file{number+1}.txt') as file:
content.append(file.read())
return content
if __name__ == '__main__':
# Asynchronous
s = time.perf_counter()
content = asyncio.run(main())
e = time.perf_counter()
print(f'Take {e - s: .3f}')
# Synchronous
s = time.perf_counter()
content = core()
e = time.perf_counter()
print(f'Take {e - s: .3f}')
得到了这个结果:
Asynchronous: Take 0.011
Synchronous: Take 0.001
为什么异步代码比同步代码需要更长的时间?
我哪里做错了?
我 post 关于 aiofiles GitHub 的一个问题 #110 和 aiofiles 的作者回答:
You're not doing anything wrong. What aiofiles does is delegate the file reading operations to a thread pool. This approach is going to be slower than just reading the file directly. The benefit is that while the file is being read in a different thread, your application can do something else in the main thread.
A true, cross-platform way of reading files asynchronously is not available yet, I'm afraid :)
希望对遇到同样问题的人有所帮助
我有一个问题 我是 python 异步世界的新手,我写了一些代码来测试 asyncio
的功能,我创建了 10 个具有随机内容的文件,名为 file1.txt, file2.txt, ..., file10.txt
这是我的代码:
import asyncio
import aiofiles
import time
async def reader(pack, address):
async with aiofiles.open(address) as file:
pack.append(await file.read())
async def main():
content = []
await asyncio.gather(*(reader(content, f'./file{_+1}.txt') for _ in range(10)))
return content
def core():
content = []
for number in range(10):
with open(f'./file{number+1}.txt') as file:
content.append(file.read())
return content
if __name__ == '__main__':
# Asynchronous
s = time.perf_counter()
content = asyncio.run(main())
e = time.perf_counter()
print(f'Take {e - s: .3f}')
# Synchronous
s = time.perf_counter()
content = core()
e = time.perf_counter()
print(f'Take {e - s: .3f}')
得到了这个结果:
Asynchronous: Take 0.011
Synchronous: Take 0.001
为什么异步代码比同步代码需要更长的时间? 我哪里做错了?
我 post 关于 aiofiles GitHub 的一个问题 #110 和 aiofiles 的作者回答:
You're not doing anything wrong. What aiofiles does is delegate the file reading operations to a thread pool. This approach is going to be slower than just reading the file directly. The benefit is that while the file is being read in a different thread, your application can do something else in the main thread.
A true, cross-platform way of reading files asynchronously is not available yet, I'm afraid :)
希望对遇到同样问题的人有所帮助