how can I make it work?

S

self.python

it's a finder using threading to accelerate but it never works..
and I have no idea why it doesn't work:(
it doesn't work too after changing threading to multiprocessing..
how can I make it work? or at least I know what's the problem...
plz help the poor newbie...



import os,threading,multiprocessing


def finder(path,q):
for x in os.walk(unicode(path)):
if x[1]:
for dirname in x[1]:
if target in dirname.lower():
q.put(os.path.join(x[0],dirname))
if x[2]:
for name in x[2]:
if target in name.lower():
q.put(os.path.join(x[0],name))

q.put(1)

def printer(q):
cmd=0
while 1:
tmp=q.get()
if tmp==1:
cmd += 1
continue
if cmd ==thnum:
break
print tmp

if __name__ =="__main__":
q=multiprocessing.JoinableQueue()
ini=os.walk(u"C:\\").next()
thnum=len(ini[1])
target=raw_input("what you wanna get\n")

p=multiprocessing.Process(target=printer,args=(q,))
p.daemon=1
p.start()

for i in xrange(thnum):
t=threading.Thread(target=finder,args=(ini[1],q,))
t.start()
print i," started"
q.join()
 
M

MRAB

it's a finder using threading to accelerate but it never works..
and I have no idea why it doesn't work:(
it doesn't work too after changing threading to multiprocessing..
how can I make it work? or at least I know what's the problem...
plz help the poor newbie...
[snip]
It would help if you gave more information than just "it doesn't work".
In what way doesn't it work?
 
S

self.python

2012ë…„ 7ì›” 9ì¼ ì›”ìš”ì¼ ì˜¤ì „ 11ì‹œ 2분 41ì´ˆ UTC+9, self.python ë‹˜ì˜ ë§:
it's a finder using threading to accelerate but it never works..
and I have no idea why it doesn't work:(
it doesn't work too after changing threading to multiprocessing..
how can I make it work? or at least I know what's the problem...
plz help the poor newbie...



import os,threading,multiprocessing


def finder(path,q):
for x in os.walk(unicode(path)):
if x[1]:
for dirname in x[1]:
if target in dirname.lower():
q.put(os.path.join(x[0],dirname))
if x[2]:
for name in x[2]:
if target in name.lower():
q.put(os.path.join(x[0],name))

q.put(1)

def printer(q):
cmd=0
while 1:
tmp=q.get()
if tmp==1:
cmd += 1
continue
if cmd ==thnum:
break
print tmp

if __name__ =="__main__":
q=multiprocessing.JoinableQueue()
ini=os.walk(u"C:\\").next()
thnum=len(ini[1])
target=raw_input("what you wanna get\n")

p=multiprocessing.Process(target=printer,args=(q,))
p.daemon=1
p.start()

for i in xrange(thnum):
t=threading.Thread(target=finder,args=(ini[1],q,))
t.start()
print i," started"
q.join()



It shows
0 started
1 started
....
22 started
but then it show nothing
the cursur blinks continuously
but the result never printed(maybe not produces?)
 
S

self.python

2012ë…„ 7ì›” 9ì¼ ì›”ìš”ì¼ ì˜¤ì „ 11ì‹œ 41분 32ì´ˆ UTC+9, self.python ë‹˜ì˜ ë§:
2012ë…„ 7ì›” 9ì¼ ì›”ìš”ì¼ ì˜¤ì „ 11ì‹œ 2분 41ì´ˆ UTC+9, self.python ë‹˜ì˜ ë§:
it's a finder using threading to accelerate but it never works..
and I have no idea why it doesn't work:(
it doesn't work too after changing threading to multiprocessing..
how can I make it work? or at least I know what's the problem...
plz help the poor newbie...



import os,threading,multiprocessing


def finder(path,q):
for x in os.walk(unicode(path)):
if x[1]:
for dirname in x[1]:
if target in dirname.lower():
q.put(os.path.join(x[0],dirname))
if x[2]:
for name in x[2]:
if target in name.lower():
q.put(os.path.join(x[0],name))

q.put(1)

def printer(q):
cmd=0
while 1:
tmp=q.get()
if tmp==1:
cmd += 1
continue
if cmd ==thnum:
break
print tmp

if __name__ =="__main__":
q=multiprocessing.JoinableQueue()
ini=os.walk(u"C:\\").next()
thnum=len(ini[1])
target=raw_input("what you wanna get\n")

p=multiprocessing.Process(target=printer,args=(q,))
p.daemon=1
p.start()

for i in xrange(thnum):
t=threading.Thread(target=finder,args=(ini[1],q,))
t.start()
print i," started"
q.join()



It shows
0 started
1 started
...
22 started
but then it show nothing
the cursur blinks continuously
but the result never printed(maybe not produces?)



I knew what's the problem on other site.
It just because I miss to send the full path to each thread...
what an idiot..
but I'm still wondering that there is any good way
 
T

Terry Reedy

it's a finder using threading to accelerate

Threading with Python does not accelerate unless all but one of the
treads are i/o bound. You need multiple processes to use multiple cores
in parallel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top