Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62809

Re: Variables in a loop, Newby question

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Variables in a loop, Newby question
Date 2013-12-27 11:39 -0500
Organization IISS Elusive Unicorn
References <9ad01eef-baf0-4018-833e-0b4dce4b9b85@googlegroups.com> <e183ed75-5ee8-429e-90b8-83e064295430@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4671.1388162351.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, 27 Dec 2013 00:53:43 -0800 (PST), vanommen.robert@gmail.com
declaimed the following:

>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


	
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 08:07 -0800
  Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 11:20 -0500
  Re: Variables in a loop, Newby question "Tobias M." <tm@tobix.eu> - 2013-12-24 17:24 +0100
  Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-24 17:29 +0100
  Re: Variables in a loop, Newby question bob gailer <bgailer@gmail.com> - 2013-12-24 12:26 -0500
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 09:54 -0800
    Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 13:10 -0500
    Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-24 13:42 -0500
    Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-25 13:35 -0500
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 10:27 -0800
    Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 02:54 +0000
      Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-25 16:42 +1100
        Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 15:27 +0000
          Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-26 12:01 +1100
          Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 12:35 +1100
            Re: Variables in a loop, Newby question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-26 16:41 +1100
              Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-26 03:14 -0500
              Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 19:24 +1100
    Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-25 15:52 +0100
    Re: Variables in a loop, Newby question Michael Torrie <torriem@gmail.com> - 2013-12-25 23:34 -0700
  Re: Variables in a loop, Newby question Larry Hudson <orgnut@yahoo.com> - 2013-12-25 00:13 -0800
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-27 00:53 -0800
    Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-27 11:39 -0500

csiph-web