Replies: 1 comment 4 replies
-
import asyncio
sample_global = 0
async_event = asyncio.Event()
async def update_globals():
global sample_global
print('Function Started...')
await async_event.wait()
print('Sleep...Before Updating...')
await asyncio.sleep(1)
print('Updating...')
sample_global += 1
print('Done Updating...')
async def foo(tasks):
await asyncio.gather(*tasks)
async def main():
tasks = [update_globals() for _ in range(5)]
asyncio.create_task(foo(tasks))
await asyncio.sleep(2) # Simulating some time before allowing updates
async_event.set()
await asyncio.sleep(2)
print(f"Final value of sample_global: {sample_global}")
if __name__ == "__main__":
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have just started exploring MicroPython specifically AsyncIO concepts.
I have written this code and tried running it in both CPython and MicroPython environments using the latest firmware but I don't know why the output is different.
The output in CPython is
Final value of sample_global: 5
But in MicroPython it is
Final value of sample_global: 0
Any hints?
Beta Was this translation helpful? Give feedback.
All reactions