We are ultimately trying to use NetWorkSpaces (NWS) as a way for Python, R, and Java agents running on different machines to
communicate and coordinate with one another.
To get started with Python NWS, we'll first have to install the NWS server, and the Python NWS client.
Next, an NWS server must be started. This can be done from a shell
using the nws command, as follows:
% twistd -ny .local/nws.tac
From another window, start an interactive Python session, and import
the NetWorkSpace class:
% python
>>> from nws.client import NetWorkSpace
Next, create a work space called 'cpe':
>>> ws = NetWorkSpace('cpe')
This is using the NWS server on the local machine. Additional arguments
can be used to specify the hostname and port used by the NWS server if
necessary.
Once we have a work space, we can write data into a variable using the
store method:
>>> ws.store('joe', 17)
The variable 'joe' now has a value of 17 in our work space.
To read that variable we use the find method:
>>> age = ws.find('joe')
which sets 'age' to 17.
Note that the find method will block until the variable 'joe' has a
value. That is important when we're trying to read that variable from a
different machine. If it didn't block, you might have to repeatedly try
to read the variable until it succeeded.
There are times when you don't want to block, but just want to see if
some variable has a value. That is done with the findTry method.
--------------------------------------------------------------------------------------
From another window, start an interactive Python session, and import
the NetWorkSpace class:
% python
>>> from nws.client import NetWorkSpace
Next, connect to a work space called 'cpe':
>>> ws = NetWorkSpace('cpe')
This won't create the 'cpe' work space, since it already exists. Now you
can execute a operation such as:
>>> x = ws.fetch('joe')
>>> x
17
you should see that you got the value 17 from the previous w.store() function;
only this time you fetched it permanently. Now try to execute the the fetch again.
>>> x = ws.fetch('joe')
Your program will now be blocked! in this session, watch it block for a minute, and then execute a 'store' in
the other session:
>>> ws.store('joe', 18)
You should see the previous session return from the fetch command and x should now be 18.
That's a basic sample of this technology, which of course can become much more complex. Keep in mind
that these programs could have been on different machines; or hosted on Amazon.
More importantly, an R program and a python program
and even a Java program could be working cooperatively.