您的位置:首页 > 编程语言 > Python开发

Python代码的多线程改造

2015-10-25 06:21 656 查看
并行化处理已经成为了很多工程项目的需求。本文展示了如何使用threadpool模块(支持Python 3.x)将普通的Python程序多线程化。

import requests
import bs4
import time
import threadpool

# build data array
data = []
for e in range(10,20):
data.append(str(e))

def print_now(request):
s = str(request.requestID)
print (s)

def crawling(link):
request_link = "http://www.wandoujia.com/search?key=" + str(link) + "&source=search"
response = requests.get(request_link)
# other code lines

# set the thread number
pool = threadpool.ThreadPool(10)
# makeRequests(some_callable, list_of_args, callback)
reqs = threadpool.makeRequests(crawling, data, print_now)
# run
[pool.putRequest(req) for req in reqs]
pool.wait()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息