Variables in a loop, Newby question

V

vanommen.robert

Hello everyone, I have been away for a while.
I have been reading all the good advises and want to explain why I want to read the temperatures separately from the main script. It takes a long timeto read out 10 temperatures. About 10 seconds. So that’s the reason why I had the idea to create a separate script and I thought by making te variables Global I could access them by other scripts. Now I know that’s not the purpose of Global.
Maybe I can create a loop that keeps running simultaneously with the rest of the script.
I’ve downloaded a great student book about Python and learning a lot.
Thanks for all the answers and I’ll post more questions in the future, I’m sure of it.
Greetings Robert
 
D

Dennis Lee Bieber

Hello everyone, I have been away for a while.
I have been reading all the good advises and want to explain why I want to read the temperatures separately from the main script. It takes a long time to read out 10 temperatures. About 10 seconds. So that’s the reason why I had the idea to create a separate script and I thought by making te variables Global I could access them by
other scripts. Now I know that’s not the purpose of Global.
Maybe I can create a loop that keeps running simultaneously with the rest of the script.
I’ve downloaded a great student book about Python and learning a lot.
Thanks for all the answers and I’ll post more questions in the future, I’m sure of it.
Greetings Robert

Check the class threading.Thread, along with Queue.Queue -- presuming
the sensor reads are blocking calls, they should have minimal effect on the
main thread...

PSEUDO-code:
-=-=-=-=-
import threading
import Queue
import time

list_of_sensors = [ whatever, is, needed, to, access, each, sensor ]
#list of sensors may just be the ID strings, or if you need to open
#each, make it a list of the opened items, so the worker doesn't
#keep opening, reading, closing, for each sensor

tempQ = Queue.Queue()

def sensorWork(retQ, list_of_sensors):
while True:
for sensor in list_of_sensors:
temp = readSensor(sensor) #presumed to be blocking
retQ.put((sensor, temp, time.time())
#if a timestamp is useful

worker = threading.Thread(target=sensorWork,
args=(tempQ, list_of_sensors))
worker.setDaemon() #lets it die when the main program exits

worker.start()

#now do the main processing
while True:
#check for available temps
numTemps = tempQ.qsize() #should be good as only one consumer
if numTemps > 0:
for i in range(numTemps - 1):
#I'm not iterating over the queue as
#that might starve the main process
#if the sensor reads produce a reading
#just fast enough to keep up with
#this loop
(sensor, temp, timestamp) = tempQ.get()
#process the temp data
#here you do the rest of the main process, which should be
#something that returns here at some regular interval so that
#the loop checks for new temp readings to be processed
 
V

vanommen.robert

Op dinsdag 24 december 2013 17:23:43 UTC+1 schreef Jean-Michel Pichavant:
----- Original Message -----
Hello, for the first time I'm trying te create a little Python
program. (on a raspberri Pi)

I don't understand the handling of variables in a loop with Python.


Lets say i want something like this.

x = 1
while x <> 10
var x = x
x = x + 1

The results must be:

var1 = 1
var2 = 2

enz. until var9 = 9

How do i program this in python?

Short story, cause it's almost xmas eve :D:

python 2.5:

var = {}
for i in range(10):
var = i

print var[1]
print var[2]
print var

var here is a dictionary. I suggest that you read through the python tutorial :)

JM


-- IMPORTANT NOTICE:

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify thesender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


This was the information I was looking for and what my first question was about. Got this working, Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,588
Members
47,210
Latest member
JuliaMulli

Latest Threads

Top