Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '#if': 0.07; 'needed,': 0.07; 'variables': 0.07; 'exits': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separately': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; '#this': 0.16; '1):': 0.16; 'blocking': 0.16; 'iterating': 0.16; 'i\xe2\x80\x99m': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scripts.': 0.16; 'seconds.': 0.16; 'temp': 0.16; 'temperatures': 0.16; 'timestamp': 0.16; 'true:': 0.16; 'whatever,': 0.16; 'student': 0.16; 'everyone,': 0.19; 'import': 0.22; 'separate': 0.22; 'script.': 0.24; 'url:home': 0.24; 'script': 0.25; 'post': 0.26; 'downloaded': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; "doesn't": 0.30; 'dec': 0.30; 'robert': 0.30; 'along': 0.30; 'lot.': 0.31; 'class': 0.32; 'regular': 0.32; 'open': 0.33; 'running': 0.33; 'fri,': 0.33; 'minimal': 0.33; 'could': 0.34; 'processed': 0.36; 'useful': 0.36; 'should': 0.36; 'list': 0.37; 'checks': 0.38; 'process,': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'read': 0.60; 'access,': 0.60; 'skip:t 30': 0.61; 'new': 0.61; 'making': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.64; 'great': 0.65; 'here': 0.66; 'reads': 0.68; 'consumer': 0.70; 'to,': 0.72; 'future,': 0.83; 'calls,': 0.84; 'each,': 0.84; 'i\xe2\x80\x99ll': 0.84; 'reading,': 0.84; 'temps': 0.84; 'items,': 0.91; 'sensors': 0.91; 'received:108': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Variables in a loop, Newby question Date: Fri, 27 Dec 2013 11:39:09 -0500 Organization: IISS Elusive Unicorn References: <9ad01eef-baf0-4018-833e-0b4dce4b9b85@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-108-68-178-129.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388162351 news.xs4all.nl 2840 [2001:888:2000:d::a6]:55479 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62809 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/