gpt4 book ai didi

python - 了解 Python 上下文变量

转载 作者:行者123 更新时间:2023-12-03 12:44:58 24 4
gpt4 key购买 nike

关于以下SO answer .为了理解使用 Contextvars 和不使用 Contextvars 之间的区别,我做了一些更改。
我希望在某个时候变量 myid被损坏,但将范围更改为更高的数字似乎根本没有影响。

import asyncio
import contextvars

# declare context var
request_id = contextvars.ContextVar('Id of request.')


async def some_inner_coroutine(myid):
# get value
print('Processed inner coroutine of myid : {}'.format(myid))
print('Processed inner coroutine of request: {}'.format(request_id.get()))
if myid != request_id.get():
print("ERROR")


async def some_outer_coroutine(req_id):
# set value
request_id.set(req_id)

await some_inner_coroutine(req_id)

# get value
print('Processed outer coroutine of request: {}'.format(request_id.get()))


async def main():
tasks = []
for req_id in range(1, 1250):
tasks.append(asyncio.create_task(some_outer_coroutine(req_id)))

await asyncio.gather(*tasks)


if __name__ == '__main__':
asyncio.run(main())
结果
Processed inner coroutine of myid   : 1
Processed inner coroutine of request: 1
Processed outer coroutine of request: 1
Processed inner coroutine of myid : 2
Processed inner coroutine of request: 2
Processed outer coroutine of request: 2
Processed inner coroutine of myid : 3
Processed inner coroutine of request: 3
Processed outer coroutine of request: 3
Processed inner coroutine of myid : 4
Processed inner coroutine of request: 4
Processed outer coroutine of request: 4
...
...
Processed inner coroutine of myid : 1244
Processed inner coroutine of request: 1244
Processed outer coroutine of request: 1244
Processed inner coroutine of myid : 1245
Processed inner coroutine of request: 1245
Processed outer coroutine of request: 1245
Processed inner coroutine of myid : 1246
Processed inner coroutine of request: 1246
Processed outer coroutine of request: 1246
Processed inner coroutine of myid : 1247
Processed inner coroutine of request: 1247
Processed outer coroutine of request: 1247
Processed inner coroutine of myid : 1248
Processed inner coroutine of request: 1248
Processed outer coroutine of request: 1248
Processed inner coroutine of myid : 1249
Processed inner coroutine of request: 1249
Processed outer coroutine of request: 1249
我应该更改什么才能看到变量 myid 的意外行为?

最佳答案

上下文变量 当您需要沿着调用链传递一个变量以便它们共享相同的上下文时很方便,在这种情况下无法通过全局变量完成 并发 . 上下文变量 可以在多线程代码和异步(使用协程)中用作全局变量的替代方案。asyncio 原生支持上下文变量并且无需任何额外配置即可使用。因为当Task创建它复制当前上下文,然后在复制的上下文中运行它的协程:

# asyncio/task.py
class Task:
def __init__(self, coro):
...
# Get the current context snapshot.
self._context = contextvars.copy_context()
self._loop.call_soon(self._step, context=self._context)

def _step(self, exc=None):
...
# Every advance of the wrapped coroutine is done in
# the task's context.
self._loop.call_soon(self._step, context=self._context)
...
以下是显示与上下文变量相比全局变量损坏的示例:
import asyncio
import contextvars

# declare context var
current_request_id_ctx = contextvars.ContextVar('')
current_request_id_global = ''


async def some_inner_coroutine():
global current_request_id_global

# simulate some async work
await asyncio.sleep(0.1)

# get value
print('Processed inner coroutine of request: {}'.format(current_request_id_ctx.get()))
if current_request_id_global != current_request_id_ctx.get():
print(f"ERROR! global var={current_request_id_global}")


async def some_outer_coroutine(req_id):
global current_request_id_global

# set value
current_request_id_ctx.set(req_id)
current_request_id_global = req_id

await some_inner_coroutine()

# get value
print('Processed outer coroutine of request: {}\n'.format(current_request_id_ctx.get()))


async def main():
tasks = []
for req_id in range(1, 10000):
tasks.append(asyncio.create_task(some_outer_coroutine(req_id)))

await asyncio.gather(*tasks)


if __name__ == '__main__':
asyncio.run(main())
输出:
...
Processed inner coroutine of request: 458
ERROR! global var=9999
Processed outer coroutine of request: 458

Processed inner coroutine of request: 459
ERROR! global var=9999
Processed outer coroutine of request: 459
...

使用 threading.local() 的转换代码示例可以在 PЕP 567 中找到

关于python - 了解 Python 上下文变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63105799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com