前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python的线程池

Python的线程池

作者头像
爬虫技术学习
发布2023-02-10 21:02:08
3030
发布2023-02-10 21:02:08
举报
文章被收录于专栏:爬虫技术学习爬虫技术学习

随便发篇文章,测试下百家号的同步功能

multiporcessing.Pool.map(fn, iterable) only accepts marshalable

代码语言:javascript
复制
# 线程池的例子
 
from multiprocessing.dummy import Pool as ThreadPool  # python2
from multiprocessing.pool import ThreadPool # python3
 
def square_number(n):
    return n ** 2
 
# function to be mapped over
def calculate_parallel(numbers, threads=2):
    pool = ThreadPool(threads)
    results = pool.map(square_number, numbers)
    pool.close()  # NOTE close before join
    pool.join()
    return results
 
if __name__ == "__main__":
    numbers = [1, 2, 3, 4, 5]
    squared_numbers = calculate_parallel(numbers, 4)
    for n in squared_numbers:
        print(n)

使用pool的一个陷阱是不太好debug, 爆出的异常往往看不清问题, 需要使用单线程调试之后再去

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 爬虫技术学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档