[Python-checkins] CVS: python/dist/src/Lib tempfile.py,1.23,1.24

Tim Peters python-dev@python.org
Fri, 12 Jan 2001 19:04:04 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv19969/python/dist/src/lib

Modified Files:
	tempfile.py 
Log Message:
Guido found a brand new race in tempfile on Linux, due to Linux changing
pid across threads (but in that case, it's still the same process, and so
still sharing the "template" cache in tempfile.py).  Repaired that, and
added a new std test.
On Linux, someone please run that standalone with more files and/or more
threads; e.g.,

    python lib/test/test_threadedtempfile.py -f 1000 -t 10

to run with 10 threads each creating (and deleting) 1000 temp files.


Index: tempfile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/tempfile.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -r1.23 -r1.24
*** tempfile.py	2001/01/12 10:02:46	1.23
--- tempfile.py	2001/01/13 03:04:02	1.24
***************
*** 68,90 ****
  
  
! _pid = None
  
  def gettempprefix():
!     """Function to calculate a prefix of the filename to use."""
!     global template, _pid
!     if os.name == 'posix' and _pid and _pid != os.getpid():
!         # Our pid changed; we must have forked -- zap the template
!         template = None
      if template is None:
!         if os.name == 'posix':
!             _pid = os.getpid()
!             template = '@' + `_pid` + '.'
!         elif os.name == 'nt':
!             template = '~' + `os.getpid()` + '-'
!         elif os.name == 'mac':
!             template = 'Python-Tmp-'
!         else:
!             template = 'tmp' # XXX might choose a better one
!     return template
  
  
--- 68,105 ----
  
  
! # template caches the result of gettempprefix, for speed, when possible.
! # XXX unclear why this isn't "_template"; left it "template" for backward
! # compatibility.
! if os.name == "posix":
!     # We don't try to cache the template on posix:  the pid may change on us
!     # between calls due to a fork, and on Linux the pid changes even for
!     # another thread in the same process.  Since any attempt to keep the
!     # cache in synch would have to call os.getpid() anyway in order to make
!     # sure the pid hasn't changed between calls, a cache wouldn't save any
!     # time.  In addition, a cache is difficult to keep correct with the pid
!     # changing willy-nilly, and earlier attempts proved buggy (races).
!     template = None
  
+ # Else the pid never changes, so gettempprefix always returns the same
+ # string.
+ elif os.name == "nt":
+     template = '~' + `os.getpid()` + '-'
+ elif os.name == 'mac':
+     template = 'Python-Tmp-'
+ else:
+     template = 'tmp' # XXX might choose a better one
+ 
  def gettempprefix():
!     """Function to calculate a prefix of the filename to use.
! 
!     This incorporates the current process id on systems that support such a
!     notion, so that concurrent processes don't generate the same prefix.
!     """
! 
!     global template
      if template is None:
!         return '@' + `os.getpid()` + '.'
!     else:
!         return template