Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update global variable in asyncio create_task

I currently have a global variable that's not being set across the application. I have two files where file2 imports from file1. The global is intialized in file1.

Here is the code that initializes the global variable and later uses it in file1.

import time
import asyncio

#Initialize global
CONNECTION_OPEN = False

async def calculate_idle(t):
    orig_time = t
    global CONNECTION_OPEN
    while True:
        await asyncio.sleep(5)
        print("GLOBAL CONNECTION", CONNECTION_OPEN)
        if CONNECTION_OPEN:
            print("This value is now true")
        else:
             print("Value is still false")

Here is the websocket code that is setting the global to true. This is located in file2.

import os
import asyncio
import websockets
import json
import threading
import time
from random import randrange
from enum import Enum
from lights import calculate_idle,CONNECTION_OPEN 

async def init_connection(message):
    #Get global variable to set
    global CONNECTION_OPEN
    global CLIENT_WS
    uri = WS_URI
    async with websockets.connect(uri) as websocket:
        print("Setting Connection open to true")
        CONNECTION_OPEN = True
        CLIENT_WS = websocket
        # send init message
        await websocket.send(message)
        print("Connection is open") 
        while CONNECTION_OPEN:
            await handleMessages(websocket, message)
        await websocket.send(json.dumps({'type': MessageType.Close.name, 'message': USERNAME}))
        await websocket.close()

Here is how this code is called in file2

async def main():
    message = json.dumps({'payload': 
                            'payload')
    loop = asyncio.get_event_loop()
    start_light = asyncio.create_task(calculate_idle(3))
    await asyncio.gather(init_connection(message), start_light)

asyncio.run(main())

The order of events is:

  • CONNECTION_OPEN is set to false
  • "Setting connectoin open to true" is printed
  • "Connection is open" is printed
  • "Value is still false" is repeatedly printed

I'd like the global variable value to be updated so that "This value is now true" is printed.

like image 367
avenmia Avatar asked Nov 22 '25 14:11

avenmia


1 Answers

This line doesn't do what you think it does:

from lights import calculate_idle,CONNECTION_OPEN 

It won't make CONNECTION_OPEN an alias for lights.CONNECTION_OPEN; it will create a new global variable (local to file2) initialized with the current value of lights.CONNECTION_OPEN.

The correct way to share a global among modules is to just import lights (which might be a good idea anyway) and use lights.CONNECTION_OPEN.

A better option is to not use a global variable at all, but to create a mutable object that contains the shared state, and pass it to the code that needs to share it. You can also add the state to an existing object needed for other things, such as an asyncio.Event, as suggested by @gold_cy.

like image 182
user4815162342 Avatar answered Nov 25 '25 02:11

user4815162342