[Python-ideas] Speed up os.walk() 5x to 9x by using file attributes from FindFirst/NextFile() and readdir()

Antoine Pitrou solipsis at pitrou.net
Wed Nov 14 20:00:55 CET 2012


On Thu, 15 Nov 2012 07:52:12 +1300
Robert Collins
<robertc at robertcollins.net> wrote:
> On Wed, Nov 14, 2012 at 11:54 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> > On 14.11.12 11:53, Robert Collins wrote:
> >>   - chdir to the directory before you stat and use a relative path: it
> >> turns out when working with many files that the overhead of absolute
> >> paths is substantial.
> >
> > Look at fwalk(). It reaches the same benefits without changing of process-global cwd (i.e. it is thread-safe).
> 
> cwd is thread-safe on unix (well, Linux anyhow :P)

Not really:

>>> print(os.getcwd())
/home/antoine/cpython/opt
>>> evt = threading.Event()
>>> def f():
...   evt.wait()
...   print(os.getcwd())
... 
>>> threading.Thread(target=f).start()
>>> os.chdir('/tmp')
>>> evt.set()
/tmp


Regards

Antoine.





More information about the Python-ideas mailing list