How to update global variable in asyncio create_task

前端 未结 2 1597
一整个雨季
一整个雨季 2021-01-26 13:48

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.

Her

相关标签:
2条回答
  • 2021-01-26 14:06

    Just use an asyncio.Event object as the global variable.

    import time
    import asyncio
    
    
    async def calculate_idle(t, conn_open_event):
        orig_time = t
        while True:
            await conn_open_event.wait()
            print("Connection is now open from idle")
    

    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
    
    async def init_connection(message, conn_open_event):
        #Get global variable to set
        global CLIENT_WS
        uri = WS_URI
        async with websockets.connect(uri) as websocket:
            print("Connection is open from socket")
            conn_open_event.set()
            CLIENT_WS = websocket
            CONNECTION_OPEN = True
            # send init message
            await websocket.send(message) 
            while CONNECTION_OPEN:
                await handleMessages(websocket, message)
            await websocket.send(json.dumps({'type': MessageType.Close.name, 'message': USERNAME}))
            await websocket.close()
    

    async def main():
        message = json.dumps({'payload': 
                                'payload')
        loop = asyncio.get_event_loop()
        event = asyncio.Event()
        start_light = asyncio.create_task(calculate_idle(3, event))
        await asyncio.gather(init_connection(message, event), start_light)
    
    asyncio.run(main())
    
    0 讨论(0)
  • 2021-01-26 14:09

    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.

    0 讨论(0)
提交回复
热议问题