[Tutor] running multiple concurrent processes

Alan Gauld alan.gauld at btinternet.com
Sat Nov 3 23:49:37 CET 2012


On 03/11/12 20:20, richard kappler wrote:

> SO... while the bot program is running, I would like it to be
> continuously cognizant of the sensor data and any changes in that data.
> Let's distill it down to a single sensor to simplify the discussion. If
> I have a temperature sensor feeding data to the arduino, which feeds
> that data through serial to the Python program (let's call that pyBrain)
> that "is" the bot, (here's the meat of the question:) how do I make the
> pyBrain constantly aware of the temperature while doing other stuff?

You could have a thread running to read the serial input and when it 
detects a message it communicates with the main program. You probably 
want an event driven framework and I think twisted will do most of what 
you want.

I suggest you go read the twisted web site and try the tutorials.
Its an event driven networking framework...

> I think what's tripping me up is that Python is sequential, yes?

Almost every programming language is sequential, certainly all the most 
popular ones. Thats what threads and subprocesses are for - to give the 
ability)or the illusion) of parallel processing.

Operating systems and GUIs work on the event driven principle where they 
have a simple program structure consisting of an event loop that just 
waits for events to arrive and dispatches the event to a separate 
thread/process to be processed.

At the kernel level instead of an event loop they rely on the hardware's 
interrupt mechanism to call the appropriate system calls.

> suddenly rises above a preset threshold of 78 degrees F how does pyBrain
> know that? Because python is sequential does there not have to be some
> specific code that tells it to periodically poll Temp1?

An event loop that polls all possible action sources and kicks off a 
separate thread/process to deal with each event that must occur in parallel.

> periodic or even continuous polling, is there some "event alert"
> function that would tell the pyBrain program to execute the "Temp1High"
> function?

Yes, you can program it yourself opr use a framework to do it for you. 
That's what GUI frameworks like Tkinter do for you (as well as provide 
the graphical UI objects etc)

Twisted as mentioned above is an event framework for monitoring multiple 
network event sources.

Twisted is not difficult to use but it is non trivial and the docs 
assume you know python fairly well.

> My initial thought was to run concurrent processes, or threads (still
> working that bit out). I have considered having Arduino write the sensor
> data to a table or dictionary or even file, just constantly overwriting
> previous values, and pyBrain could pull values out of that
> table/dictionary/file as needed.

That's another common pattern - especially in old COOL mainframes!
But its easy to create bottlenecks and lose any pretence of real-time 
multi tasking.

But don't be surprised to discover that much of the apparent parallelism 
in computing is nothing but sleight-of-hand trickery relying on the 
speed of electronics. A computer can perform a great many things in, 
say, a tenth of a second. And a computer updating 10 displays every 
second looks a lot like parallel processing to a human.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list