Python: Importing a variable inside of a infinite loop

I have two modules, a host and a scanner. Both loop indefinitely to communicate with the serial ports. I want to import the variable "bestchannel" from scanner into host but by importing it, the while loop inside scanner runs first and loops forever. I want each module to run separately but be able to send each other data in real time. Is this possible?

(outside of scanning ram)

Sample Code:

Host Loop----------------------------------------------
while True:
    ser.write( assemble("20","FF","FF","64","B") )
    sData = ser.read(100)
    if len(sData)>0:
        for i in range(0, len(sData)-17):
            if sData[i]==chr(1) and sData[i+1]==chr(20) and sData[i+2]==chr(int("A1", 16)):
                height = (ord(sData[i+16])*256+ord(sData[i+17]))/100
                print "Sensor ", ord(sData[i+12]), " is returning height ",
                height, "mm. The minnoisechan:",minchannel

Scanner Loop----------------------------------------------
while True:
    ser.write( scan("FF", "FF", str(scanlength)) )  #Channel Mask, Length
    time.sleep(scanlength+2.0)
    sData = ser.read(100)
    if len(sData)>0:
        for i in range(0, len(sData)-16):
            if sData[i]==chr(1) and sData[i+1]==chr(23) and sData[i+2]==chr(int("C5", 16)):
                for j in range(0, 16):
                    chan[j] = sData[i+5+j]
                    print "channel: ",j+11,"=",ord(chan[j])
                    if ord(chan[j])<minvalue:
                        minvalue=ord(chan[j])
                        minchannel=j+11
                count+=1
                print "count",count,"minvalue:",minvalue,"minchannel:",minchannel
                minvalue=999

I want minchannel from scanner to be accessible to host.

sample code is in the link or down in the answer, sorry I had to use a different browser.

Answers


So again, if you haven't explored implementing your code using threads then I'd suggest that to get two loops to run at the same time. So something like this:

import threading
import Queue

def host(dataQueue):
    """
    Host code goes here.
    """
    # Check dataQueue for incoming data among other things...
    ....

def scanner(dataQueue):
    """
    Scanner code goes.
    """
    # Put data into dataQueue among other things...
    ....

if __name__ == 'main':
    dataQ = Queue.queue()
    hostThread = threading.Thread(target=host, name="Host", arg=(dataQ,))
    scannerThread = threading.Thread(target=scanner, name="Scanner", arg=(dataQ,))
    hostThread.start()
    scannerThread.start()

At the very least this will get you started running your two processes together. You still will need to figure out the thread management aspect of this.


Need Your Help

jQuery preventDefault function not working on jsfiddle

javascript jquery

I cannot get the following simple jQuery to work. I know I am overlooking something, so please help me out. Here is the code at http://jsfiddle.net/Z5waA/. It's a simple click the button, then aler...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.