第二章:数据结构-queue线程安全的FIFO实现-构建一个多线程播客客户程序

2.6.4 构建一个多线程播客客户程序
这一节将构建一个播客客户程序,程序的源代码展示了如何利用多个线程使用Queue类。这个程序要读入一个或多个RSS提要,对每个提要的专辑排队,显示最新的五集以供下载,并使用线程并行地处理多个下载。这里没有提供完备的错误处理,所以不能在实际的生成环境中使用,不过这个框架实现可以作为一个很好的例子来说明如何使用Queue模块。
首先要建立一些操作参数。一般情况下,这些参数来自用户输入(例如,首选项、数据库等)。不过在这个例子中,线程数和要获取的URL列表都使用了硬编码值。

from queue import Queue
import threading
import time
import urllib
from urllib.parse import urlparse

import feedparser  # 第三方库,需要安装

# Set up some global variables.
num_fetch_threads = 2
enclosure_queue = Queue()
# A real app wouldn't use hard-coded data.
feed_urls = [
    'htpp://talkpython.fm/episodes/rss',
    ]

def message(s):
    print('{}:{}'.format(threading.current_thread().name,s))

# 函数download_enclosures()在工作线程中运行,使用urllib处理下载。

def download_enclosures(q):
    """This is the worker thread function.
    It processes items in the queue one after
    another.These daemon threads go into an
    infinite loop,and exit only when
    the main thread ends.
    """
    while True:
        message('looking for the next enclosure')
        url = q.get()
        filename = url.rpartition('/')[-1]
        message('downloading {}'.format(filename))
        response = urllib.request.urlopen(url)
        data = response.read()
        # Save the downloaded file to the current directory.
        message('writing to {}'.format(filename))
        with open(filename,'wb') as outfile:
            outfile.write(data)
        q.task_done()

# 一旦定义了线程的目标函数,接下来便可以启动工作线程。download_enclosures()
# 处理语句url = q.get()时,会阻塞并等待,直到队列返回某个结果。这说明,即使
# 队列中没有任何内容也可以安全地启动线程。

# Set up some threads to fetch the enclosures.
for i in range(num_fetch_threads):
    worker = threading.Thread(
        target=download_enclosures,
        args=(enclosure_queue,),
        name='worker-{}'.format(i),
        )
    worker.setDaemon(True)
    worker.start()

# 下一步使用feedparser模块获取提要内容,并将这些专辑的URL入队。一旦第一个URL
# 增加到队列,就会有某个工作线程提取这个URL,并且开始下载。这个循环会继续增加
# 元素,直到这个提要已被完全消费,工作线程会依次将URL出队以完成下载。

# Download the feed(s) and put the enclosure URLs into the queue.
for url in feed_urls:
    response = feedparser.parse(url,agent='fetch_podcasts.py')
    for entry in response['entries'][:5]:
        for enclosure in entry.get('enclosures',[]):
            parsed_url = urlparse(enclosure['url'])
            message('queuing {}'.format(
                parsed_url.path.rpartition('/')[-1]))
            enclosure_queue.put(enclosure['url'])

# 还有一件事情要做,要使用join()再次等待队列清空。

# Now wait for the queue to be empty,indicating that we have
# processed all of the downloads.
message('*** main thread waiting')
enclosure_queue.join()
message('*** done')

具体的输出取决于所使用的RSS提要的内容。
运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值