I run this code in jupyter notebook(python 3.6.8). I expect the code to print 2 as the result, somehow it still prints 1. I want to know why.
import asyncio
x = 1
async def func():
global x
x = 2
print(x)
await func()
print(x)
The jupyter notebook environment I am using is:
If you want to change a global variable in IPython
coroutine you have to acquire the asyncio.Lock
.
import asyncio
x = 1
async def func():
global x
lock = asyncio.Lock()
await lock.acquire()
x = 2
lock.release()
await func()
print(x) # 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.