Quick update
I found a way to solve this in webMathematica, using CollectStreams
. But still, below problem leaves me puzzled, I would very much like to learn how this works.
Original
This question is motivated by the fact that I started to used webMathematica and observed a (to me) strange behavior there. I tried to isolate the problem, in plain Mathematica. I think it is rather closely related to this question, so I hope it is not considered a duplicate.
Setup
We start with a fresh kernel and check the Streams
:
Quit[]
Streams[]
{OutputStream["stdout", 1], OutputStream["stderr", 2]}
Next, we load some CountryData
related info, such as:
CountryData["CH", "Population"]
7.595*10^6
Checking Streams
again:
Streams[]
{OutputStream["stdout", 1], OutputStream["stderr", 2], InputStream[ "/Users/myusername/Library/Mathematica/Paclets/Repository/\ CountryData-8.0.62/Data/CountryData.wdx", 111]}
Ok, CountryaData
opened a new stream to read the info from. All good.
Now we Close
that stream manually:
Close[Last@Streams[]]
so we're back to
Streams[]
{OutputStream["stdout", 1], OutputStream["stderr", 2]}
and then we try to get the population a different country:
CountryData["FR", "Population"]
General::openx: InputStream[/Users/myusername/Library/Mathematica/Paclets/Repository/CountryData-8.0.62/Data/CountryData.wdx,111] is not open. >>
BinaryRead::openx: InputStream[/Users/myusername/Library/Mathematica/Paclets/Repository/CountryData-8.0.62/Data/CountryData.wdx,112] is not open. >>
`$Failed
and interestingly, this still works:
CountryData["CH", "Population"]
My Conclusion
It seems to me that on a fresh kernel, the first time we call CountryData
(and e.g. ElementData
, ...) it opens a stream and subsequently remembers that it has (should have) an open stream and tries to read from it. If that stream gets closed (which seems to happen in webMathematica, e.g. on reload of a page), you get $Failed
.
My Question
Do you know where CountryData
stores the stream it is associated with, in the open session?
Is there a way to "reset" CountryData
without quitting the kernel?
Or can I add the lost stream manually? I tried various possiblities, such as:
OpenRead["/Users/myusername/Library/Mathematica/
Paclets/Repository/CountryData-8.0.62/Data/CountryData.wdx"]
but I got the wrong serial number - and CountryData
was still referring to the previous one (111
in above example).
Note Please let me know if the webMathematica code I used to play with is of interest (how to replicate etc)