summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-01-13 03:04:02 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-01-13 03:04:02 (GMT)
commit9fadfb0d1dbf1b42efbe1374ea55f99b57b76a3f (patch)
tree23c348d668b1045f52c2d3eedfa3b9d308eb3e34 /Lib/tempfile.py
parentd7b68021ceb241fed4c082bc7a6585a987c0aab4 (diff)
downloadcpython-9fadfb0d1dbf1b42efbe1374ea55f99b57b76a3f.zip
cpython-9fadfb0d1dbf1b42efbe1374ea55f99b57b76a3f.tar.gz
cpython-9fadfb0d1dbf1b42efbe1374ea55f99b57b76a3f.tar.bz2
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.
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py47
1 files changed, 31 insertions, 16 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 3e1e08d..8ac707d 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -67,25 +67,40 @@ def gettempdir():
return tempdir
-_pid = None
+# 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."""
- 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
+ """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:
- 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
+ return '@' + `os.getpid()` + '.'
+ else:
+ return template
def mktemp(suffix=""):