Using global state variables in coroutines?

Viewed 737

I'm building a discord bot using the discord.py library - all user interaction therefore necessarily takes place in coroutines, defined with async and called with await.

One of my functions is going to require a saved state variable - a time offset used in a calculation that will occasionally need to be updated manually by users.

I can't use a normal global variable in the main thread - the coroutines can't see them. What's a sensible design pattern for preserving a state variable between multiple coroutines?

2 Answers

Maybe u can work with a dict. For example:

x = {the name of the member:[stored variables]}

U can change the variable with:

x[variable u want to change] = variable u want to give it

More about dicts

You can make your own thread safe class that wraps the state and use it like an in memory data store

Related