用于网络任务的Python序列多线程



我正在尝试运行一个具有多个线程的脚本,以减少脚本完成所需的时间。我需要知道如何在这样的程序中实现多线程。

脚本示例:

def getnetworkdata():
   data = ["somesite.com/1", "somesite.com/2", "somesite.com/3", "somesite.com/4"]
   for url in data:
      r = requests.get(url)
      someOtherArray.append(r.text)

线程应该按所需任务的顺序运行。

我期待的输出:

someOtherArray = [1, 2, 3, 4]

我使用的是Python 2.x

from multiprocessing.dummy import Pool as ThreadPool
data = ["somesite.com/1", "somesite.com/2", "somesite.com/3", "somesite.com/4"]
# Make the Pool of workers
pool = ThreadPool(4)
# Open the urls in their own threads
# and return the results
results = pool.map(requests.get, data)
someOtherArray = map( lambda x: x.text, results )
#close the pool and wait for the work to finish 
pool.close()
pool.join()

最新更新