473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

os walk() and threads problems (os.walk are thread safe?)

Hello list,

I have a strange problem with os.walk and threads in python script. I
have one script that create some threads and consume Queue. For every
value in Queue this script run os.walk() and printing root dir. But if i
increase number of threads the result are inconsistent compared with one
thread.

For example, run this code plus sort with one thread and after run again
with ten threads and see diff(1).

--code--
#!/usr/local/bin/python

import os, time, glob
import Queue
import threading

EXIT=False
POOL=Queue.Queue(0)
NRO_THREADS=1
#NRO_THREADS=10

class Worker(threading.Thread):
def run(self):
global POOL, EXIT
while True:
try:
mydir=POOL.get(timeout=1)
if mydir == None:
continue

for root, dirs, files in os.walk(mydir):
print root

except Queue.Empty:
if EXIT:
break
else:
continue
except KeyboardInterrupt:
break
except Exception:
raise

for x in xrange(NRO_THREADS):
Worker().start()
try:
for i in glob.glob('/usr/ports/*'):
POOL.put(i)

while not POOL.empty():
time.sleep(1)
EXIT = True

while (threading.activeCount() 1):
time.sleep(1)
except KeyboardInterrupt:
EXIT=True
--code--

If someone can help with this i appreciate.

Regards

--
Marcus Alves Grando
marcus(at)sbh.eng.br | Personal
mnag(at)FreeBSD.org | FreeBSD.org
Nov 13 '07 #1
4 3386
Marcus Alves Grando wrote:
Hello list,

I have a strange problem with os.walk and threads in python script. I
have one script that create some threads and consume Queue. For every
value in Queue this script run os.walk() and printing root dir. But if i
increase number of threads the result are inconsistent compared with one
thread.

For example, run this code plus sort with one thread and after run again
with ten threads and see diff(1).
I don't see any difference. I ran it with 1 and 10 workers + sorted the
output. No diff whatsoever.

And I don't know what you mean by diff(1) - was that supposed to be some
output?

Diez
Nov 13 '07 #2
Diez B. Roggisch wrote:
Marcus Alves Grando wrote:
>Hello list,

I have a strange problem with os.walk and threads in python script. I
have one script that create some threads and consume Queue. For every
value in Queue this script run os.walk() and printing root dir. But if i
increase number of threads the result are inconsistent compared with one
thread.

For example, run this code plus sort with one thread and after run again
with ten threads and see diff(1).

I don't see any difference. I ran it with 1 and 10 workers + sorted the
output. No diff whatsoever.
Do you test in one dir with many subdirs? like /usr or /usr/ports (in
freebsd) for example?
>
And I don't know what you mean by diff(1) - was that supposed to be some
output?
No. One thread produce one result and ten threads produce another result
with less lines.

Se example below:

@@ -13774,8 +13782,6 @@
/usr/compat/linux/proc/44
/usr/compat/linux/proc/45
/usr/compat/linux/proc/45318
-/usr/compat/linux/proc/45484
-/usr/compat/linux/proc/45532
/usr/compat/linux/proc/45857
/usr/compat/linux/proc/45903
/usr/compat/linux/proc/46

Regards

--
Marcus Alves Grando
marcus(at)sbh.eng.br | Personal
mnag(at)FreeBSD.org | FreeBSD.org
Nov 13 '07 #3
Marcus Alves Grando wrote:
Diez B. Roggisch wrote:
>Marcus Alves Grando wrote:
>>Hello list,

I have a strange problem with os.walk and threads in python script. I
have one script that create some threads and consume Queue. For every
value in Queue this script run os.walk() and printing root dir. But if i
increase number of threads the result are inconsistent compared with one
thread.

For example, run this code plus sort with one thread and after run again
with ten threads and see diff(1).

I don't see any difference. I ran it with 1 and 10 workers + sorted the
output. No diff whatsoever.

Do you test in one dir with many subdirs? like /usr or /usr/ports (in
freebsd) for example?
Yes, over 1000 subdirs/files.
>>
And I don't know what you mean by diff(1) - was that supposed to be some
output?

No. One thread produce one result and ten threads produce another result
with less lines.

Se example below:

@@ -13774,8 +13782,6 @@
/usr/compat/linux/proc/44
/usr/compat/linux/proc/45
/usr/compat/linux/proc/45318
-/usr/compat/linux/proc/45484
-/usr/compat/linux/proc/45532
/usr/compat/linux/proc/45857
/usr/compat/linux/proc/45903
/usr/compat/linux/proc/46
I'm not sure what that directory is, but to me that looks like the
linux /proc dir, containing process ids. Which incidentially changes
between the two runs, as more threads will have process id aliases.

Try your script on another directory.

Diez
Nov 13 '07 #4
Diez B. Roggisch wrote:
Marcus Alves Grando wrote:
>Diez B. Roggisch wrote:
>>Marcus Alves Grando wrote:

Hello list,

I have a strange problem with os.walk and threads in python script. I
have one script that create some threads and consume Queue. For every
value in Queue this script run os.walk() and printing root dir. But if i
increase number of threads the result are inconsistent compared with one
thread.

For example, run this code plus sort with one thread and after run again
with ten threads and see diff(1).
I don't see any difference. I ran it with 1 and 10 workers + sorted the
output. No diff whatsoever.
Do you test in one dir with many subdirs? like /usr or /usr/ports (in
freebsd) for example?

Yes, over 1000 subdirs/files.
Strange, because to me accurs every time.
>
>>And I don't know what you mean by diff(1) - was that supposed to be some
output?
No. One thread produce one result and ten threads produce another result
with less lines.

Se example below:

@@ -13774,8 +13782,6 @@
/usr/compat/linux/proc/44
/usr/compat/linux/proc/45
/usr/compat/linux/proc/45318
-/usr/compat/linux/proc/45484
-/usr/compat/linux/proc/45532
/usr/compat/linux/proc/45857
/usr/compat/linux/proc/45903
/usr/compat/linux/proc/46

I'm not sure what that directory is, but to me that looks like the
linux /proc dir, containing process ids. Which incidentially changes
between the two runs, as more threads will have process id aliases.
My example are not good enough. I run this script in ports directory of
freebsd and imap folders in my linux server, same thing.

@@ -182,7 +220,6 @@
/usr/ports/archivers/p5-POE-Filter-Bzip2
/usr/ports/archivers/p5-POE-Filter-LZF
/usr/ports/archivers/p5-POE-Filter-LZO
-/usr/ports/archivers/p5-POE-Filter-LZW
/usr/ports/archivers/p5-POE-Filter-Zlib
/usr/ports/archivers/p5-PerlIO-gzip
/usr/ports/archivers/p5-PerlIO-via-Bzip2
@@ -234,7 +271,6 @@
/usr/ports/archivers/star-devel
/usr/ports/archivers/star-devel/files
/usr/ports/archivers/star/files
-/usr/ports/archivers/stuffit
/usr/ports/archivers/szip
/usr/ports/archivers/tardy
/usr/ports/archivers/tardy/files

Regards

--
Marcus Alves Grando
marcus(at)sbh.eng.br | Personal
mnag(at)FreeBSD.org | FreeBSD.org
Nov 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
1
by: Chuck | last post by:
I am creating a pub/sub broker implementation in C# and I am having some trouble understanding how I should best implement the invoke portion in order to make sure it is as thread-safe as it can...
9
by: Arafangion | last post by:
Hello, recently I've been trying to figure out how the heck to just destroy a thread. I have since rewritten the code in question, but what I was trying to do was to use the .Abort() method, but...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.