-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_web_demo.py
100 lines (83 loc) · 2.3 KB
/
pull_web_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# 多进程操作
# 从服务器上读取json文件,解析json文件,
# 根据文件中的option属性决定是add还是delete
# 若为add,则读取文件中的图片的url,拉到本地人脸数据库中
# 若为delete,则删除数据库中的相应图片
import requests
import json
import urllib2
import os
import time
from PyQt4 import QtCore
dir = './web_dataset/'
pull = requests.get("http://xx.xx.xx.xx/api/deviceTest?deviceId=yyy")
print pull.text
jsonData = json.loads(pull.text, encoding='utf-8')
l = len(jsonData['list'])
print l
# start = time.time()
# for i in range(1,l):
# json = jsonData['list'][i]
# userID = json['userId']
# name = json['name']
# option = json['option']
# url = json['url']#获取第一张图片
# print url
#
# req=urllib2.urlopen(str(url))
# buf = req.read()
#
# file_name = str(userID) +'_'+ name + '.jpg'
# file_dir = os.path.join(dir, file_name)
#
# if option == 'add':
#
# print file_dir
# f=open(file_dir,"wb")
# req=urllib2.urlopen(url)
# buf=req.read()
# f.write(buf)
# elif option == 'delete':
# if os.path.exists(file_dir):
# os.remove(file_dir)
# else:
# print "no such file:%s"%file_dir
# end =time.time()
# print(u'Use time1 {0} s'.format(end - start))
from multiprocessing import Pool
import multiprocessing
from multiprocessing import Process
def Pull(i):
print "this is func{0}:{1}".format(i, time.ctime())
json = jsonData['list'][i]
userID = json['userId']
name = json['name']
option = json['option']
url = json['url'] # 获取第一张图片
print url
req = urllib2.urlopen(str(url))
buf = req.read()
file_name = str(userID) + '_' + name + '.jpg'
file_dir = os.path.join(dir, file_name)
if option == 'add':
print file_dir
f = open(file_dir, "wb")
req = urllib2.urlopen(url)
buf = req.read()
f.write(buf)
elif option == 'delete':
if os.path.exists(file_dir):
os.remove(file_dir)
else:
print "no such file:%s" % file_dir
print "func{0} finish{1}".format(i, time.ctime())
start = time.time()
pool = Pool(processes=10)
for i in range(1, l):
pool.apply_async(Pull, (i,))
pool.close()
pool.join()
end = time.time()
print(u'Use time2 {0} s'.format(end - start))