RE: Re: Re: Re: my garbage collector sucks

"Michael Geary" <[email protected]>
Newsgroups gmane.comp.lang.prothon.user
Message-ID <[email protected]>
> From: Mark Hahn
> Your example looked like it opened 10000 files because I saw
> no close.  More complicated code than your example could have
> an open file get stuck in a reference cycle and never be closed.

Paul's example *does* open up to 10000 files, as he mentioned. It only
happens to avoid opening all these file on CPython because of an
implementation detail in the memory manager.

> From: Paul Prescod
> 
> for i in range(0, 10000):
> 	file = open(str(i)+".txt", "w").write("Hello world")
> 
> Maybe there is no *computer program* that can see that this 
> would have different behaviour in Python versus Jython/Java 
> but I can see it with my own two eyes. In Jython it will open 
> somewhere between 1 and 10000 files simultaneously and in 
> Python there will never be more than two files open at a time.

> From: Mark Hahn
> ... I don't want to encourage the use of finalizers.  
> If you want this kind of functionality use the explicit 
> try/finally statements.  That is what they are there for.
> If you want to come up with some new explicit finalizer 
> equivalent that is scope-based, feel free.

At the risk of sounding like a broken record, this is what code blocks were
made for. :-)

A direct translation of the above code into Ruby would have the same
problem:

for i in 0...10000 do
    File.open( "#{i}.txt", "w" ).write( "Hello world" )
end

But you wouldn't do it that way. You'd use a code block:

for i in 0...10000 do
    File.open( "#{i}.txt", "w" ) { |file| file.write "Hello world" }
end

Or (same code, different style):

for i in 0...10000 do
    File.open( "#{i}.txt", "w" ) do |file|
        file.write "Hello world"
    end
end

If you provide a code block to a File.open call, it opens the file, calls
the code block with the file as an argument, and when the code block returns
it closes the file.

It's all very deterministic; nothing depends on what kind of memory
management the interpreter uses.

-Mike
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.