0

I have a program which runs a camera for a set number of exposures and a set length of exposures through a serial port. The program talks to a server which gives error values, if the error values are within an acceptable range, then the exposure starts and a countdown clock runs until the exposure ends. The following variables are used to make this happen.

T="Length of exposure in seconds" N="Number of exposures"

then a while loop runs the program using the following

    def countdown(n):
       for i in range(int(n)):
        c = int(n) - int(i)
        print c ,'seconds left','\r',
        time.sleep(1)  

While x < T loop: 
        countdown(n)

I would like to run a thread which is constantly probing the error number from the server, and if the error number grows to large, it changes to the value of i to equal n.

def errortest():
                    test=struct.pack("B",10)
                    s.send(test)
                    data = s.recv(BUFFER_SIZE)
                    if ord(data) < (75) and ord(data) > 0:
                        print ord(data)
                        time.sleep(5)
                    else:
                        print ("error! Stopping exposure")
                        i=n

My problem is that the variables aren't being shared between the functions. I had some success with setting "i" and "n" as global variables, but this has caused other problems as well, depending on the order in which I invoke or write the different functions. I've also tried return i=n, but n is not shared. It is especially a problem as both threads are running concurrently and not sharing variables. This is why I cannot get queue to work because it pauses the for loop at each instance of q.get().

Is there a better way than using global variables to share variable values between functions running as concurrent threads?

1 Answer 1

1

You might want to try sharing your variables within a class. You'll find many tutorials if you google for them. If you want to distribute your tasks into different programs, xmlrpc is a simple possibility.

Sign up to request clarification or add additional context in comments.

2 Comments

Just to make sure I'm understanding classes. Classes are essentially local environments that are analogous to the global environment? Ie I set a global variable and all functions in the environment can use that variable. I set a class variable and all functions within that class can see that variable?
Basically yes and it is a very useful concept. Each variable in Python is the instance of a class. E.g. a string variable has other functions and local variables as a list or a number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.