[pypy-dev] a bug of pypy:don't auto close the file handler.

Greg Bowyer gbowyer at fastmail.co.uk
Wed Jan 11 07:09:25 CET 2012


You never close the file, so you are depending on CPythons ref-counting 
GC, which will collect the object when it goes out of scope.

Pypys default GC is mark / sweep, so it will not close the file when the 
file goes out of scope but rather at some _arbiterry_ future point.

Putting the file in a with statement will do the right thing, as will 
closing it. Gritty details can be found in the docs
http://doc.pypy.org/en/latest/cpython_differences.html#differences-related-to-garbage-collection-strategies


On 10/01/2012 21:35, 聂宝鹏 wrote:
> description:
> pypy don't close file opened in a function.the the program exit after 
> run a while because it open too
> many files which exceed the limited of system.
> the code is :
> #!/usr/bin/env python
> import os,time,signal
> last = {}
> now = {}
> def stack(s,f):
> global now,last
> last = now
> now = {}
> lines=open("/proc/net/dev","rt").readlines()
> #f=open("/proc/net/dev")
> #lines=f.readlines()
> #f.close()
> ls=lines[2:] #del the header line
> for m in range(0,len(ls)):
> l=ls[m] # l = per line
> name,l=l.split(':') # name = dev ,l = other;
> l=l.split() # l = other.split items.
> bytes_in = l[0]
> bytes_out = l[8]
> packets_in = l[1]
> packets_out = l[9]
> now[name]={}
> now[name]['bytes_in']=bytes_in
> now[name]['bytes_out']=bytes_out
> now[name]['packets_in']=packets_in
> now[name]['packets_out']=packets_out
> if last:
> print '-'*80
> for name in now.iterkeys():
> print "dev: 
> %s\tbytes:\trx:%.2fMb/s\ttx:%.2fMb/s\tpackets\trx:%s\ttx:%s" % (name, 
> int ( int(now[name]['bytes_in']) - int(last[name]['bytes_in']) 
> )/1024.0/1024.0*8.0,int(int(now[name]['bytes_out'])-int(last[name]['bytes_out']))/1024.0/1024.0*8.0 
> ,
> int ( int(now[name]['packets_in']) - int(last[name]['packets_in']) 
> ),int(int(now[name]['packets_out'])-int(last[name]['packets_out'])) )
> print '-'*80
> if __name__=='__main__':
> signal.signal(signal.SIGALRM,stack)
> signal.setitimer(signal.ITIMER_REAL,0.01,1.0)
> while 1 :
> signal.pause()
>
> _______________________________________________
> pypy-dev mailing list
> pypy-dev at python.org
> http://mail.python.org/mailman/listinfo/pypy-dev



More information about the pypy-dev mailing list