Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use requests_futures to sync #2

Merged
merged 1 commit into from
Dec 16, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions tmpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
"gz": "application/gzip"}

RE_JSON_BLK = r'^\{"h":(?P<h>\{.+?\}),"t":(?P<t>\[.+?\]),"v":(?P<v>\[.+?\])\}$'
DBG_TMPO_SINK_GET = "Request block | time:%.3f sid:%s rid:%d lvl:%2d bid:%d"
DBG_TMPO_SINK_STORE = \
"Store block | time:%.3f sid:%s rid:%d lvl:%2d bid:%d size[B]:%d"
DBG_TMPO_SINK = "time:%.3f sid:%s rid:%d lvl:%2d bid:%d size[B]:%d"
EPOCHS_MAX = 2147483647

Expand All @@ -109,7 +112,8 @@
import math
import time
import sqlite3
import requests
import requests_futures.sessions
import concurrent.futures
import zlib
import re
import json
Expand Down Expand Up @@ -142,7 +146,8 @@ def __init__(self, path=None):
self.dbcur.execute(SQL_SENSOR_TABLE)
self.dbcur.execute(SQL_TMPO_TABLE)
self.dbcon.commit()
self.rqs = requests.Session()
self.rqs = requests_futures.sessions.FuturesSession(
executor=concurrent.futures.ThreadPoolExecutor(max_workers=10))
self.rqs.headers.update({"X-Version": "1.0"})

def add(self, sid, token):
Expand Down Expand Up @@ -260,28 +265,36 @@ def _rqsync(self, sid, rid, lvl, bid):
"rid": rid,
"lvl": lvl,
"bid": bid}
r = self.rqs.get(
f = self.rqs.get(
API_TMPO_SYNC % (self.host, sid),
headers=headers,
params=params,
verify=self.crt)
r = f.result()
fs = []
for t in r.json():
self._rqblock(sid, token, t["rid"], t["lvl"], t["bid"], t["ext"])
fs.append((t,self._rqblock(sid, token, t["rid"], t["lvl"], t["bid"], t["ext"])))
for (t,f) in fs:
self._store_block(f.result(), sid, t["rid"], t["lvl"], t["bid"], t["ext"])

def _rqblock(self, sid, token, rid, lvl, bid, ext):
headers = {
"Accept": HTTP_ACCEPT["gz"],
"X-Token": token}
r = self.rqs.get(
f = self.rqs.get(
API_TMPO_BLOCK % (self.host, sid, rid, lvl, bid),
headers=headers,
verify=self.crt)
self._dprintf(DBG_TMPO_SINK_GET, time.time(), sid, rid, lvl, bid)
return f

def _store_block(self, r, sid, rid, lvl, bid, ext):
blk = sqlite3.Binary(r.content)
now = time.time()
self.dbcur.execute(SQL_TMPO_INS, (sid, rid, lvl, bid, ext, now, blk))
self.dbcon.commit()
self._clean(sid, rid, lvl, bid)
self._dprintf(DBG_TMPO_SINK, now, sid, rid, lvl, bid, len(blk))
self._dprintf(DBG_TMPO_SINK_STORE, now, sid, rid, lvl, bid, len(blk))

def _clean(self, sid, rid, lvl, bid):
if lvl == 8:
Expand Down