相关疑难解决方法(0)

如何在 asyncio create_task 中更新全局变量

我目前有一个未在整个应用程序中设置的全局变量。我有两个文件,其中 file2 从 file1 导入。全局在 file1 中初始化。

这是初始化全局变量并稍后在 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")
Run Code Online (Sandbox Code Playgroud)

这是将全局设置为 true 的 websocket 代码。它位于文件 2 中。

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): …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio

0
推荐指数
1
解决办法
2199
查看次数

标签 统计

python ×1

python-3.x ×1

python-asyncio ×1