python - asyncio 任务是否共享内存?
问题描述
我的 Python 应用程序中有多个异步函数。
这些函数执行读取文件内容、操作和转换为二进制的操作
例如:
async def perform_all(location):
file_contents = await get_file(location)
new_file_contents = await manipulate_file(file_contents)
in_binary = await convert_to_binary(new_file_contents)
await save(location + '.binary', in_binary)
所有这些单独的功能都是异步的:
async def get_file(location):
# ... open file, read all and send contents
async def manipulate_file(file_contents):
# ... do something to the contents like add their lengths together
async def convert_to_binary(new_file_contents):
# ... given the characters, convert to binary and return it
async def save(location, in_binary):
# ... save binary in the location
在应用程序启动时,我创建了 5 个任务。
每个任务在perform_all()
不同的位置运行函数。该函数perform_all()
由在任务中运行的循环每 1 分钟执行一次。它查找文件并将其转换为二进制文件。
如果可以回答,我有几个问题:
这段代码是异步安全的吗?
如果不是那么为了理解,我如何使它成为非异步安全的,以便我可以可视化问题?
这两个任务是否会错误地操作相同的文件内容,或者每个任务都会分配给它自己的内存并且一个任务中的文件内容与
file_contents
另一个任务中的文件内容不同?
解决方案
推荐阅读
- python - ImportError:没有名为“flask”的模块 python3 ubuntu 16.04
- javascript - JS 正则表达式匹配两种可能的组合
- c# - 下载和缓存离线数据(图像/视频/3d 模型)的代码架构应该是什么?
- haskell - 在 Haskell 中实例化 Num 类
- amazon-web-services - 如果我使用 COPY 命令将数据从 S3 加载到 Redshift,它会遵循我的 dist 样式和键吗?
- sql-server - Azure Sql 表分区性能
- php - Laravel5.2:按用户时区发送队列电子邮件?
- java - Android - 如何确定加载到 ImageView 的位图何时显示?
- python - Python 多处理:通过管理器更新共享实例属性。一般方法不起作用
- python - CNN用于python中的特征数组