免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3976 | 回复: 9
打印 上一主题 下一主题

Python 线程池 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-09 20:19 |只看该作者 |倒序浏览
在IBM网站里找到了一个Python 线程池的例子。
我在执行这个例子,程序却挂起了。哪位大侠能帮忙看看到底是怎么回事吗?

import Queue
import threading
import urllib2
import time

hosts = ["http://www.sina.com.cn/", "http://www.163.com/", "http://www.sohu.com/","http://ibm.com.cn", "http://apple.com.cn"]

queue =Queue.Queue(0)

class ThreadUrl(threading.Thread):
#threaded url Grab
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue =queue

    def run(self):
        while True:
            host =self.queue.get()   #从队列queue取出web地址
            url =urllib2.urlopen(host)
            print url.read(1024)        #打印每个web页面前1024个字节
            

            #signals to queue job is done
            self.queue.task_done()

start =time.time()

def main():
    #spawn a pool of threads,and pass them queue instance
    for i in range(5):
        t =ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

    #populate queue with data
    for host in hosts:
        print host
        queue.put(host)

    #wait on the queue until everything has been processed
    queue.join()


main()
print "Elapsed Time: %s" % (time.time() -start)

每次执行时,5个线程都创建了,但每个现在在执行到 run()的红色代码处时停止了
搞不明白了

论坛徽章:
0
2 [报告]
发表于 2012-04-09 21:26 |只看该作者
1. 我简单的运行了下这个程序,但没发现执行到 run()停止的现象,请你确定下你是否访问hosts的中url列表,请断点调试一下
2. 我简单说下你这个程序的问题:
   (1) while死循环一定要sleep,否则CPU占用率是个问题。
   (2) queue是的共享内存,每个线程都可以访问,一定要用锁。
3. 请简单看下如下描述:
  1. task_done()
  2. Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

  3. If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

  4. Raises a ValueError if called more times than there were items placed in the queue.
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-04-10 11:23 |只看该作者
看了下, 不会存在太大的问题,可以正常运行。不过对这程序作了简单的修改了下。
http://bbs.python520.com/thread-51-1-1.html  这里帖了下代码。

论坛徽章:
0
4 [报告]
发表于 2012-04-10 12:58 |只看该作者
目测没问题, urllib2.urlopen加个timeout参数看看是不是有线程导致的堵塞

论坛徽章:
0
5 [报告]
发表于 2012-04-10 16:06 |只看该作者
回复 2# 追忆的风筝


    谢谢你的建议。另外我想问一下,调试时可以跟踪指定的线程吗?我是在PythonWin里调试的

论坛徽章:
0
6 [报告]
发表于 2012-04-10 16:07 |只看该作者
回复 3# python520com


    谢谢,论坛不错

论坛徽章:
0
7 [报告]
发表于 2012-04-10 16:25 |只看该作者
回复 5# xusancu

可以是可以,不过比较麻烦,试想如果你程序中开了100+个线程,那调试起来可就麻烦了,你可以在程序中添加指定的“条件”加以辅助调试,其次我认为Wing IDE断点调试比较不错,而且不怎么占用内存,可以多开几个,唯一的缺点就是中文支持不太好,仅供参考。
   

论坛徽章:
0
8 [报告]
发表于 2012-04-11 19:50 |只看该作者
对于Queue在thread中用,我通常是会加锁的, 否则可能会互锁而挂住的

论坛徽章:
0
9 [报告]
发表于 2012-04-11 22:07 |只看该作者
tj0120 发表于 2012-04-11 19:50
对于Queue在thread中用,我通常是会加锁的, 否则可能会互锁而挂住的


python queue的实现里本身就加了锁, 不用再加, 事实上, 就连python的list, append和pop都是原子的

论坛徽章:
0
10 [报告]
发表于 2012-04-11 22:13 |只看该作者
Queue 里面是不要加锁的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP