/
main.py
80 lines (66 loc) · 2.08 KB
/
main.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
import Queue
import json
import time
import datetime
import threading
from iniconf import PlateIni
from watch_file import WatchFile
import gl
from upload import app, db, Upload
basepath = 'C:\SpreadData\ImageFile'
class Uploader(threading.Thread):
def __init__(self,t_name):
threading.Thread.__init__(self)
self.que = gl.CREATEDQUE
self.ini = PlateIni()
self.is_quit = False
self.t_name = t_name
def __del__(self):
print '%s quit', t_name
del self.ini
def run(self):
while 1:
try:
item = gl.CREATEDQUE.get(timeout=1)
p = self.ini.get_plate(item['path'])
if item['fresh']:
t = time.mktime(time.strptime(p['datetime'], '%Y-%m-%d %H:%M:%S'))
query = Upload.insert(timestamp=int(t), uploadflag=True,
path=item['path'],
plateinfo=json.dumps(p))
query.execute()
else:
query = Upload.update(uploadflag=True).where(Upload.id == item['id'])
query.execute()
print datetime.datetime.now(), self.t_name, item['path']
time.sleep(1)
except Queue.Empty:
pass
except Exception as e:
print e
time.sleep(1)
finally:
if self.is_quit:
break
def loop_histroy():
while 1:
if gl.CREATEDQUE.empty():
query = Upload.select().where(Upload.uploadflag == False).limit(5)
for i in query:
gl.CREATEDQUE.put({'fresh': False, 'path': i.path, 'id': i.id})
time.sleep(2)
if __name__ == "__main__":
db.connect()
w = WatchFile(path='.')
for i in range(4):
t = Uploader(str(i))
t.setDaemon(True)
t.start()
#loop_histroy()
while 1:
try:
time.sleep(1)
except KeyboardInterrupt:
break
del w
db.close()