Python 多进程_进程池_多线程_线程池实现比较
来源:互联网 发布:淘宝手抓饼哪个牌子好 编辑:程序博客网 时间:2024/05/21 07:15
多进程:
#!/usr/bin/env python
#coding:gbk
#================================================================
#Name : GetFileFromWeb.py
#Desc : Get File From Web
#Author : zalzhou
#Date : 20121031
#Modify :
#From :
#Desc : 同步10个进程,md5列表分割10份
#================================================================
import os
import sys
import time
import urllib
import random
import multiprocessing
md5List = []
Num = 10
def GetMd5List():
for i in range(Num):
tmpList = []
md5List.append(tmpList)
curPath = os.path.abspath(os.path.curdir)
filePath = curPath + "\\md5.txt"
f = open(filePath,'r')
for line in f.readlines():
i = random.randint(0,9)
md5List[i].append(line.strip())
f.close
def GetFileFromList(md5List):
for md5 in md5List:
GetFileFromMd5(md5)
def GetFileFromMd5(md5):
curPath = os.path.abspath(os.path.curdir)
for i in range(5):
j = random.randint(0,2)
if j == 0:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.150/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
if j == 1:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.151/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
if j == 2:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.152/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
time.sleep(1)
if __name__ == '__main__':
GetMd5List()
for i in range(Num):
p = multiprocessing.Process(target=GetFileFromList,args=(md5List[i],))
p.start()
#p.join()
进程池:
#!/usr/bin/env python
#coding:gbk
#================================================================
#Name : GetFileFromWeb.py
#Desc : Get File From Web
#Author : zalzhou
#Date : 20121031
#Modify :
#From :
#Desc : 异步10个进程,md5列表分割10份
#================================================================
import os
import sys
import time
import urllib
import random
import multiprocessing
md5List = []
Num = 10
def GetMd5List():
for i in range(Num):
tmpList = []
md5List.append(tmpList)
curPath = os.path.abspath(os.path.curdir)
filePath = curPath + "\\md5.txt"
f = open(filePath,'r')
for line in f.readlines():
i = random.randint(0,9)
md5List[i].append(line.strip())
f.close
def GetFileFromList(md5List):
for md5 in md5List:
GetFileFromMd5(md5)
def GetFileFromMd5(md5):
curPath = os.path.abspath(os.path.curdir)
for i in range(5):
j = random.randint(0,2)
if j == 0:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.150/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
if j == 1:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.151/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
if j == 2:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.152/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
time.sleep(1)
if __name__ == '__main__':
GetMd5List()
proc_pool = multiprocessing.Pool(Num)
pmap = dict(proc_pool.map(GetFileFromList,md5List,len(md5List)/Num))
proc_pool.close()
#proc_pool.join()
多线程:
#!/usr/bin/env python
#coding:gbk
#================================================================
#Name : GetFileFromWeb.py
#Desc : Get File From Web
#Author : zalzhou
#Date : 20121031
#Modify :
#From :
#Desc : 同步10个线程,md5列表分割10份
#================================================================
import os
import sys
import time
import string
import urllib
import random
import threading
md5List = []
Num = 10
def GetMd5List():
for i in range(Num):
tmpList = []
md5List.append(tmpList)
curPath = os.path.abspath(os.path.curdir)
filePath = curPath + "\\md5.txt"
f = open(filePath,'r')
for line in f.readlines():
j = random.randint(0,9)
md5List[j].append(line.strip())
f.close
class GetFileFromWeb(threading.Thread):
def __init__(self,md5List):
threading.Thread.__init__(self)
self.md5List = md5List
self.curPath = os.path.abspath(os.path.curdir)
def GetFileFromMd5(self,md5):
local = os.path.join(self.curPath,md5)
for i in range(5):
j = random.randint(0,2)
if j == 0:
try:
url = r'http://123.138.163.150/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
elif j == 1:
try:
url = r'http://123.138.163.151/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
elif j == 2:
try:
url = r'http://123.138.163.152/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
def run(self):
for md5 in self.md5List:
self.GetFileFromMd5(md5)
if __name__ == '__main__':
GetMd5List()
for i in range(Num):
t = GetFileFromWeb(md5List[i])
t.start()
线程池:
#!/usr/bin/env python
#coding:gbk
#================================================================
#Name : GetFileFromWeb.py
#Desc : Get File From Web
#Author : zalzhou
#Date : 20121031
#Modify :
#From :
#Desc : 异步10个线程,md5列表分割20份
#================================================================
import os
import sys
import time
import urllib
import random
import threading
import Queue
md5List = []
Num = 10
queue = Queue.Queue()
def GetMd5List():
for i in range(20):
tmpList = []
md5List.append(tmpList)
curPath = os.path.abspath(os.path.curdir)
filePath = curPath + "\\md5.txt"
f = open(filePath,'r')
for line in f.readlines():
i = random.randint(0,19)
md5List[i].append(line.strip())
f.close
class GetFileFromWeb(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
md5List = self.queue.get()
self.Work(md5List)
self.queue.task_done()
def GetFileFromMd5(self,md5):
curPath = os.path.abspath(os.path.curdir)
for i in range(5):
j = random.randint(0,2)
if j == 0:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.150/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
if j == 1:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.151/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
if j == 2:
try:
local = os.path.join(curPath,md5)
url = r'http://123.138.163.152/download?md5='+md5+'&start=0&end=0&key=NONE'
urllib.urlretrieve(url,local,)
print "get file\r\n"
break
except:
print "try again\r\n"
continue
time.sleep(1)
def Work(self,md5List):
for md5 in md5List:
self.GetFileFromMd5(md5)
if __name__ == '__main__':
GetMd5List()
for i in range(Num):
t = GetFileFromWeb(queue)
t.setDaemon(True)
t.start()
for md5 in md5List:
queue.put(md5)
queue.join()
- Python 多进程_进程池_多线程_线程池实现比较
- android_java_进程_线程
- python基础-信号量Semaphore(进程_线程)、事件Event(进程_线程)
- 01_进程和线程
- Python_进程_线程篇
- 多线程_进程概述及多进程的意义
- 代码_多进程_简单实例
- 3.3 进程管理_线程在Linux中的实现
- 进阶9_多线程3_线程池的基本
- 进程/线程通信_信号量与互斥锁_笔记
- 09_张孝祥_多线程_java5线程并发库的应用_线程池
- 20170530——进程_多进程_共享数据
- 学习笔记_进程 vs. 线程
- 20170906_线程和进程的区别
- 20170925_线程和进程的区别
- 学习笔记_多进程
- 【WebScraping】并行下载_多线程爬虫&多进程爬虫
- 进程_任务数据结构
- 简明H.264率失真分析概念
- Win7下抓包工具安装坎坷
- JavaScript eval()和JSON
- [设计模式]学习设计模式之二(原则1)
- Flex 可编辑的 Spark List 组件
- Python 多进程_进程池_多线程_线程池实现比较
- z-index 使用 混乱 错误 的原因
- 获取iPhone手机上话筒的声音
- android 2.3 电源管理
- 多文件上传带进度条的
- MyEclipse插件的编码设置(新手查看)
- 计算机多用户登录(扫盲)
- android
- 电子政务IT运维管理发展方向探讨