1

I'm trying to read a variable from one script into another script. Both scripts are in the same folder. In the script where I read the variable I only get the initial value of that variable (0x00). How I can update the variables across those scripts so I can read the variables from script1 in script2?

Below script1

from flask import Flask, render_template, request, redirect 
        
#Define variables #(This are the values i read in another script and they never update)
byte_1 = 0x00
byte_2 = 0x00 
byte_3 = 0x00
byte_4 = 0x00
        
        
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
        
def home():
                 
    #Make the variables global 
    global byte_1
    global byte_2
    global byte_3
    global byte_4
            
    if request.method == 'GET':
        return render_template('home.html')
        
    if request.method == 'POST':
        for i, value in request.form.items():
                
            buttons(i, value)     #Call function to update the variables 
                    
    return redirect(request.url)  #Redirect to refresh page
          
          
          
def buttons(i, value):
            
    #Make the variables global
    global byte_1
    global byte_2
    global byte_3
    global byte_4
            
    #Compare "i" and "value" from "request.form.items" and modify variable byte_1 to byte_4 
    #This are the values i want to read from another script2 but i only read 0x00?
    if i == ("K01") and value ==("On"):    #Set bit (Bitwise operation: or)
        byte_4 |= 0b10000000
    if i == ("K01") and value ==("Off"):   #Reset bit (Bitwise operation: and)
        byte_4 &= 0b01111111    
    #/// and so on     
           
#Run server
if __name__ == "__main__":
   app.run(host='192.168.1.117', port=5000, debug=True)

Below is script 2. Here I want to read variables byte_1 to byte_4 from script1 but only read 0x00:

import script1
while True:

    print(script1.byte_1)
    print(script1.byte_2)
    print(script1.byte_3)
    print(script1.byte_4)
Dale K
  • 25,246
  • 15
  • 42
  • 71
Underscore
  • 13
  • 4

1 Answers1

0

I'm not sure, if the solution you are looking for exists or is reasonable.

In order to change a certain value and use it across your REST service, you should rather use some kind of data store. This could be a file you store on your system, but this bears a high risk, due to conflicting access. Optimally, you would us a database, which allows transactional access and therefore helps to avoid concurrency problems.

Using global variables is considered a bad practice for several reasons. Hence, it should be avoided. See this question for more in depth information about this topic: Why are global variables evil?.

Furthermore, doing something what you try violates one of the key principles of the REST protocol - namely its statelessness. Ususally, a REST request is a stateless operation, meaning there is no state maintained after the requests is done. In your code the variables are maintained throughout the time of the server being run meaning there is a state of your application.

I would suggest, you have a look into sqlite, in order to store the values in a simple database and retrieve them from there. It's a great lightweight database system, which also supports in-memory databases. In-memory databases are highly performant, but also volatile since their content is only stored within RAM instead of the hard disk.

schilli
  • 1,700
  • 1
  • 9
  • 17
  • Indeed working with a file to store data on the system isn't a good solution. There were too much issues with formatting and messy code also. About the REST protocol I'm aware of it. That's the reason the continues script will process those variables further. Storing the variables in a database is a good solution to me. I was planning to add a sql database anyway for other purposes so adding those variables is perfect. When working with a sql database there is no need for global variables too i guess. Will try to get rid of them. Thanks for the fast reply and clear explanation. – Underscore May 29 '21 at 06:55