2007年05月09日 星期三 20:32
СµÜдÁ˸ö¶àÏ̲߳âÊÔ³ÌÐò£¬Ä¿µÄÏëÈÃÿ¸öÏ̶߳¼Óлú»á¶ÁconfigItemsÀïµÄÌõÄ¿£¬µ«ÊÇÕâ¸ö³ÌÐòµÄÔËÐнá¹ûÖ»ÓÐthread-1ÔÚ¶Á¡£²»ÖªµÀÊDz»ÊÇËøÔËÓõIJ»¶Ô£¬Çë´ó´óÃÇÖ¸½Ì°¡¡£
import time
from threading import *
configItems = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n']
class testThread(Thread):
def __init__(self, some_lock):
Thread.__init__(self)
self.lock = some_lock
def run(self):
global configItems
lock = self.lock
lock.acquire()
while len(configItems)>0:
item = configItems[0]
del configItems[0]
print item, self.getName()
time.sleep(2)
lock.release()
if __name__ == "__main__":
the_lock = Lock()
threads=[]
threadCount = 5
for i in xrange(threadCount):
t = testThread(the_lock)
t.start()
threads.append(t)
for i in xrange(threadCount):
threads[i].join()
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070509/5ddb1f6b/attachment.html
2007年05月09日 星期三 20:43
On 5/9/07, hch <huangchonggnu在gmail.com> wrote: > 小弟写了个多线程测试程序,目的想让每个线程都有机会读configItems里的条目,但是这个程序的运行结果只有thread-1在读。不知道是不是锁运用的不对,请大大们指教啊。 > > import time > from threading import * > configItems = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', > 'm', 'n'] > class testThread(Thread): > def __init__(self, some_lock): > Thread.__init__(self) > self.lock = some_lock > def run(self): > global configItems > lock = self.lock > lock.acquire() > while len(configItems)>0: > item = configItems[0] > del configItems[0] > print item, self.getName() > time.sleep(2) > lock.release() > if __name__ == "__main__": > the_lock = Lock() > threads=[] > threadCount = 5 > for i in xrange(threadCount): > t = testThread(the_lock) > t.start() > threads.append(t) > for i in xrange(threadCount): > threads[i].join() 我想应该是锁的问题。你把加锁解锁的处理放在了循环外面,这样直接循环结束别人才有机会进行处理。但从你处理的过程来看,一个循环就可以全部处理掉,因此别人根本没有机会处理。应该把锁处理放在循环里面。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年05月09日 星期三 21:24
Ð޸ĴúÂëºó£¬
while len(configItems)>0:
lock.acquire()
item = configItems[0]
del configItems[0]
print item, self.getName()
time.sleep(2)
lock.release()
µÃµ½µÄÊÇÕâ¸ö½á¹û£º
a Thread-1
b Thread-1
c Thread-1
d Thread-1
e Thread-1
f Thread-1
g Thread-1
h Thread-1
i Thread-1
j Thread-1
k Thread-1
l Thread-1
m Thread-1
n Thread-1
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/local/lib/python2.5/threading.py", line 460, in __bootstrap
self.run()
File "multithreads.py", line 15, in run
item = configItems[0]
IndexError: list index out of range
¿´À´ÆäËûÏß³ÌÒ²ÓвÎÓë½øÀ´ÁË¡£¿ÉÊÇ×îÖÕ»ñÈ¡µ½itemµÄ¶¼»¹ÊÇThread-1£¬Èç¹ûÎÒÏë¿´µ½±ðµÄÏß³ÌÒ²ÄÜ»ñÈ¡µ½item£¬¸ÃÔõôµ÷ÊÔÄØ£¿ÎÒÊÔ׿ӴóÁËconfigItemsµÄÈÝÁ¿£¬±ÈÈç˵¼Ó´óµ½ÁËÉϰÙÌõ£¬¿É¶¼ÊÇThread-1»ñÈ¡µÄ¡£
ÔÚ07-5-9£¬limodou <limodou在gmail.com> дµÀ£º
>
> On 5/9/07, hch <huangchonggnu在gmail.com> wrote:
> >
> СµÜдÁ˸ö¶àÏ̲߳âÊÔ³ÌÐò£¬Ä¿µÄÏëÈÃÿ¸öÏ̶߳¼Óлú»á¶ÁconfigItemsÀïµÄÌõÄ¿£¬µ«ÊÇÕâ¸ö³ÌÐòµÄÔËÐнá¹ûÖ»ÓÐthread-1ÔÚ¶Á¡£²»ÖªµÀÊDz»ÊÇËøÔËÓõIJ»¶Ô£¬Çë´ó´óÃÇÖ¸½Ì°¡¡£
> >
> > import time
> > from threading import *
> > configItems = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
> 'l',
> > 'm', 'n']
> > class testThread(Thread):
> > def __init__(self, some_lock):
> > Thread.__init__(self)
> > self.lock = some_lock
> > def run(self):
> > global configItems
> > lock = self.lock
> > lock.acquire()
> > while len(configItems)>0:
> > item = configItems[0]
> > del configItems[0]
> > print item, self.getName()
> > time.sleep(2)
> > lock.release()
> > if __name__ == "__main__":
> > the_lock = Lock()
> > threads=[]
> > threadCount = 5
> > for i in xrange(threadCount):
> > t = testThread(the_lock)
> > t.start()
> > threads.append(t)
> > for i in xrange(threadCount):
> > threads[i].join()
>
>
> ÎÒÏëÓ¦¸ÃÊÇËøµÄÎÊÌâ¡£Äã°Ñ¼ÓËø½âËøµÄ´¦Àí·ÅÔÚÁËÑ»·ÍâÃæ£¬ÕâÑùÖ±½ÓÑ»·½áÊø±ðÈ˲ÅÓлú»á½øÐд¦Àí¡£µ«´ÓÄã´¦ÀíµÄ¹ý³ÌÀ´¿´£¬Ò»¸öÑ»·¾Í¿ÉÒÔÈ«²¿´¦Àíµô£¬Òò´Ë±ðÈ˸ù±¾Ã»Óлú»á´¦Àí¡£Ó¦¸Ã°ÑËø´¦Àí·ÅÔÚÑ»·ÀïÃæ¡£
>
> --
> I like python!
> UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
> My Blog: http://www.donews.net/limodou
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070509/ccfa6163/attachment-0001.htm
2007年05月09日 星期三 21:31
On 5/9/07, hch <huangchonggnu在gmail.com> wrote: > 修改代码后, > while len(configItems)>0: > lock.acquire() > item = configItems[0] > del configItems[0] > print item, self.getName() > time.sleep(2) > lock.release() > 得到的是这个结果: release应该放到sleep(2)前,这样让别人有机会,这样别人还是没有机会。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年05月09日 星期三 21:46
it works, thanks :) ÔÚ07-5-9£¬limodou <limodou在gmail.com> дµÀ£º > > On 5/9/07, hch <huangchonggnu在gmail.com> wrote: > > Ð޸ĴúÂëºó£¬ > > while len(configItems)>0: > > lock.acquire() > > item = configItems[0] > > del configItems[0] > > print item, self.getName() > > time.sleep(2) > > lock.release() > > µÃµ½µÄÊÇÕâ¸ö½á¹û£º > > releaseÓ¦¸Ã·Åµ½sleep(2)ǰ£¬ÕâÑùÈñðÈËÓлú»á£¬ÕâÑù±ðÈË»¹ÊÇûÓлú»á¡£ > > -- > I like python! > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > My Blog: http://www.donews.net/limodou > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070509/84884314/attachment.html
2007年05月09日 星期三 21:54
ÎÒºÜÐÀÉÍÄãµÄ¶ÔÏó±à³Ì·ç¸ñ~£¡ ÎÒÀÏÊÇÓú¯Êýʽ±à³Ì·ç¸ñµÄ£¨¿ÉÄÜÊÇѧ¹ýC°É£©£¬ºÜÏëѧһ϶ÔÏó±à³Ì·ç¸ñ On 5/9/07, hch <huangchonggnu在gmail.com> wrote: > > > СµÜдÁ˸ö¶àÏ̲߳âÊÔ³ÌÐò£¬Ä¿µÄÏëÈÃÿ¸öÏ̶߳¼Óлú»á¶ÁconfigItemsÀïµÄÌõÄ¿£¬µ«ÊÇÕâ¸ö³ÌÐòµÄÔËÐнá¹ûÖ»ÓÐthread-1ÔÚ¶Á¡£²»ÖªµÀÊDz»ÊÇËøÔËÓõIJ»¶Ô£¬Çë´ó´óÃÇÖ¸½Ì°¡¡£ > > import time > from threading import * > configItems = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', > 'm', 'n'] > class testThread(Thread): > def __init__(self, some_lock): > Thread.__init__(self) > self.lock = some_lock > def run(self): > global configItems > lock = self.lock > lock.acquire() > while len(configItems)>0: > item = configItems[0] > del configItems[0] > print item, self.getName() > time.sleep(2) > lock.release() > if __name__ == "__main__": > the_lock = Lock() > threads=[] > threadCount = 5 > for i in xrange(threadCount): > t = testThread(the_lock) > t.start() > threads.append(t) > for i in xrange(threadCount): > threads[i].join() > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- ×¢ÒâÉíÌ壬ÉíÌåÊǸïÃüµÄ±¾Ç®£¡£¡ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070509/c034b232/attachment.htm
2007年05月10日 星期四 14:20
ºÃÏñÓöàÏ̻߳ù±¾¶¼ÕâÑù°É£¿ ¼Ì³ÐThreadÈ»ºóÔÚrunÖÐдÈëÄÚÈÝ µ±È»Èç¹û¶«Î÷ÉÙ»¹ÊÇ¿ÉÒÔÓú¯Êý·½Ê½ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070510/ba37ff97/attachment.html
Zeuux © 2025
京ICP备05028076号