Python论坛  - 讨论区

标题:[python-chinese] 多线程同步问题求助

2007年05月09日 星期三 20:32

hch huangchonggnu在gmail.com
星期三 五月 9 20:32:13 HKT 2007

СµÜдÁ˸ö¶àÏ̲߳âÊÔ³ÌÐò£¬Ä¿µÄÏëÈÃÿ¸öÏ̶߳¼Óлú»á¶Á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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月09日 星期三 20:43

limodou limodou在gmail.com
星期三 五月 9 20:43:06 HKT 2007

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月09日 星期三 21:24

hch huangchonggnu在gmail.com
星期三 五月 9 21:24:21 HKT 2007

Ð޸ĴúÂëºó£¬
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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月09日 星期三 21:31

limodou limodou在gmail.com
星期三 五月 9 21:31:38 HKT 2007

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月09日 星期三 21:46

hch huangchonggnu在gmail.com
星期三 五月 9 21:46:33 HKT 2007

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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月09日 星期三 21:54

jessinio smith jessinio在gmail.com
星期三 五月 9 21:54:49 HKT 2007

ÎÒºÜÐÀÉÍÄãµÄ¶ÔÏó±à³Ì·ç¸ñ~£¡

ÎÒÀÏÊÇÓú¯Êýʽ±à³Ì·ç¸ñµÄ£¨¿ÉÄÜÊÇѧ¹ý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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月10日 星期四 14:20

风向标 vaneoooo在gmail.com
星期四 五月 10 14:20:50 HKT 2007

ºÃÏñÓöàÏ̻߳ù±¾¶¼ÕâÑù°É£¿
¼Ì³ÐThreadÈ»ºóÔÚrunÖÐдÈëÄÚÈÝ

µ±È»Èç¹û¶«Î÷ÉÙ»¹ÊÇ¿ÉÒÔÓú¯Êý·½Ê½
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070510/ba37ff97/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号