Is python dictionary async safe?(PYTHON词典异步安全吗?)
本文介绍了PYTHON词典异步安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在Python应用程序中创建了一个字典,我在其中保存数据,并且我有两个任务,这两个任务并行运行并从外部API获取数据。一旦获得数据,他们就会更新字典-每个字典中都有一个不同的键。
我想了解字典是否是异步安全的,或者在读取/更新字典时需要加锁吗?
每次任务还读取上次保存的值。
my_data = {}
asyncio.create_task(call_func_one_coroutine)
asyncio.create_task(call_func_two_coroutine)
async def call_func_one_coroutine():
data = await goto_api_get_data()
my_data['one'] = data + my_data['one']
async def call_func_two_coroutine():
data = await goto_api_another_get_data()
my_data['two'] = data + my_data['two']
推荐答案
异步基于协作多任务,只能在显式我想了解字典是否是异步安全的,或者在读取/更新字典时需要加锁吗?
await
表达式或async with
和async for
语句上切换任务。因为单个字典的更新永远不会涉及等待(等待必须在更新开始之前完成),所以就异步多任务而言,它实际上是原子的,您不需要锁定它。这适用于从异步代码访问的所有数据结构。
再举一个没有问题的例子:
# correct - there are no awaits between two accesses to the dict d
key = await key_queue.get()
if key in d:
d[key] = calc_value(key)
字典修改不是异步安全的示例将涉及以await
s分隔的对字典的多次访问。例如:
# incorrect, d[key] could appear while we're reading the value,
# in which case we'd clobber the existing key
if key not in d:
d[key] = await read_value()
若要更正该错误,您可以在await
后面添加另一个检查,或者使用显式锁定:
# correct (1), using double check
if key not in d:
value = await read_value()
# Check again whether the key is vacant. Since there are no awaits
# between this check and the update, the operation is atomic.
if key not in d:
d[key] = value
# correct (2), using a shared asyncio.Lock:
async with d_lock:
# Single check is sufficient because the lock ensures that
# no one can modify the dict while we're reading the value.
if key not in d:
d[key] = await read_value()
这篇关于PYTHON词典异步安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:PYTHON词典异步安全吗?


猜你喜欢
- 沿轴计算直方图 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01