Channels ▼
RSS

Design

Python NetWorkSpaces and Parallel Programs

Source Code Accompanies This Article. Download It Now.


Coordinated Binding Behavior

Looking again at x = y, what if y has not yet been defined? With conventional sequential programs, we could get junk data. A more helpful language might signal an error or raise an exception; after all, nobody else is going to come along and define y for us.

But in an ensemble setting, somebody might well do just that. In other words, in the context of coordination, an unbound name has a perfectly valid (and useful) interpretation: "Please hold."

Now consider:

x = 123
x = 456

In the setting of conventional sequential programs, it would be unusual to see such code (and a compiler might warn about or eliminate the dead code), but it's reasonable to let the second assignment simply overwrite 123 with 456. We're the only process in town, so who else is going to care about the old value?

But in ensemble settings, other processes may be interested in the sequence of values bound to x. If so, how do we know a particular value of x has been put to good use?

Enter generative communication. Some coordination events generate data that exist independent of any process, others consume such data. We interpret variable binding as adding a value to a list of values, rather than overwriting a single value. We do so by maintaining a FIFO queue of values. But how do we shed values? To complete the picture, retrieval of a value bound to a name removes one value from the queue. In short, an assignment records a value of interest, a retrieval consumes one value, and an empty list of values triggers "Please hold" for a retrieval.

To see how well these play together in a Python session, run this "worker" code:

from nws.client import NetWorkSpace
def f(x): return x*x*x
ws = NetWorkSpace('table')
while True: 
  ws.store('r', f(ws.fetch('x')))

Then, in another Python session, run the "master":

ws = NetWorkSpace('table')
for x in range(100): ws.store('x', x)
for x in range(100): print 'f(%d) = %d'%(x, ws.fetch('r'))

You will see printed a list of numbers and their cubes, computed by the worker:

  • The master and worker can be started in either order.
  • The number of workers is not specified in the master code and is completely flexible, but a modest code change would be needed to print out the results in correct order if more than one worker is used.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video